1

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?

3
  • So you want output to be array containing fujitsu, 30001 only ?? Commented Dec 10, 2013 at 10:40
  • I assume fujitsu, 3, 0, 0, 0, 1 Commented Dec 10, 2013 at 10:42
  • Please note that fujitsu30001 refers to the CONSTANT with that name. 'fujitsu30001' or "fujitsu30001" refers to a STRING with that content. Commented Dec 10, 2013 at 11:16

3 Answers 3

2

Try this:

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

http://ideone.com/9H65fW

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.