Question

I'm using preg_split to split a value. I need to store each value of the split into an array.

currently what is being passed in to the preg split is:

preg_split("/[0-9]/", fujitsu30001 , NULL , PREG_SPLIT_OFFSET_CAPTURE);

I need it to split on each number so it appears as:

[fujitsu, 3, 0, 0, 0, 1]

in an array, any help would be great on this matter.

Would it be better to use preg_match?

Was it helpful?

Solution

Try this:

preg_split("/(?=[0-9])/", 'fujitsu30001');

http://ideone.com/9H65fW

OTHER TIPS

You should use matching instead of splitting:

preg_match_all('/[a-z]+|\d/i', 'fujitsu30001', $matches);
print_r($matches[0]);

The expression matches either:

  1. a sequence of letters or,
  2. a single digit.

This is repeated until it reaches the end of the subject.

I managed to do it via preg_split:

$regex= "#([0-9])#";
$string = 'fujitsu30001';                    
$array = preg_split($regex, $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

which split the string on hitting a number so it appeared as

fujitsu, 3, 0, 0, 0, 1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top