简体   繁体   中英

PHP preg_split in to array

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 ?

Try this:

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

http://ideone.com/9H65fW

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM