views:

1607

answers:

4

I need to split my string input into an array at the commas.

How can I go about accomplishing this?

Input:

9,[email protected],8
+9  A: 

Try explode


$myArray = explode(',', $myString);
mgroves
A: 

$arr= split (",",$mystring);

http://php.net/split

Haim Evgi
`split()` uses regular expressions so it will be intuitively slower than `explode()`
Ian Elliott
I would try to use the pcre extension instead of the unsupported, old and buggy builtin regular expressions. preg_split would be the better choice.
soulmerge
If you *must* use regular expression, that is :)
soulmerge
+2  A: 
$string = '9,[email protected],8';
$array = explode(',', $string);

For more complicated situations, you may need to use preg_split.

ceejayoz
+2  A: 

If that string comes from a csv file, I would use fgetcsv() (or str_getcsv() if you have PHP V5.3). That will allow you to parse quoted values correctly. If it is not a csv, explode() should be the best choice.

soulmerge