2

I have a string with some numbers and text and I'm trying to split the string at the first non-numeric character.

For Example, I have a few strings like

$value = '150px';
$value = '50em';
$value = '25%';

I've been trying to split the string using preg_split and a little regex.

$value_split = preg_split( '/[a-zA-Z]/' , $fd['yks-mc-form-padding'] );

I'm able to get the first part of the string using $value_split[0], for example I can store 150, or 50 or 25. I need to return the second part of the string as well (px, em or %).

How can I split the string using preg_split or something similar to return both parts of the array??

Thanks!

EHerman
  • 1,852
  • 7
  • 32
  • 52

3 Answers3

4

If you want to use regex and you haven't already, you should play with RegExr.

To do what you're wanting with regex, assuming all the strings will be all numeric together, followed by all non-numeric, you could do:

$matches = array();
preg_match('/([0-9]+)([^0-9]+)/',$value,$matches);

Then $matches[1] will be the numeric part and $matches[2] will be the rest

To break it down,
[0-9] matches any numeric character, so [0-9]+ matches 1 or more numeric characters in a row, so per the docs $matches[1] will have the (numeric) text matched in by the first set of parentheses

and [^0-9] matches any non-numeric character, so [^0-9]+ matches 1 or more non-numeric characters in a row and fills $matches[2] because it's in the 2nd set of parentheses

Reed
  • 14,703
  • 8
  • 66
  • 110
  • Wow, seems to work very well. I'll have a play around with it and see if it is consistent. Thanks Jakar, and thanks for the RegExr site resource, I hadn't seen that before! – EHerman Sep 04 '14 at 18:27
  • You're welcome. As long as all of the `$value`s are numeric characters only, followed by non-numeric characters only (like 123a%b), then it will work 100% of the time. – Reed Sep 04 '14 at 18:49
  • 1
    good, but try using `\d` and `\D` instead of `[0-9]` and `[^0-9]` – pguardiario Sep 05 '14 at 01:45
3

By preg_split() you cannot achieve what are you trying to. It will delete the part of your string which separates the whole string (in this case it will be separated by character [a-zA-Z]). Use preg_match() (or preg_match_all()) function.

You can use this pattern:

/([0-9]+)([a-zA-Z%]+)/

See demo.

Lkopo
  • 4,798
  • 8
  • 35
  • 60
  • using `[^0-9]` for the 2nd match is more universal than `[a-zA-Z_%@#]` because it matches ANY non-numeric character instead of just the few specified. Given the use case, though, it probably won't matter either way. – Reed Sep 04 '14 at 19:04
  • 1
    I totally agree with you, but I don't want to look like I am copying you :). But for this case it is irrelevant due to he wanted 3 values probably for CSS which have 3 formats (px, em and %). So characters such as `_@#` are not important here. I will edit. – Lkopo Sep 04 '14 at 19:10
1

Use the PREG_SPLIT_OFFSET_CAPTURE flag - it will cause an array to be returned, with item [0] being the string matched, and item [1] its starting position in the original string. You can then use that info to extract the rest of the string by using ordinary sub-string functionality. Something along the lines of:

$values_split = preg_split( '/[a-zA-Z]/' , $fd['yks-mc-form-padding'] );
$position = $values_split[0][1]
$length = $values_split[0][0]
$startPos = $position + $length
$numToGet = lenght($input) - $startPos

$remainder = substr($inline, startPos, $numToGet)
ventsyv
  • 3,316
  • 3
  • 27
  • 49