How can I split a comma delimited string into an array?

03/26/2020 12:30:01

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

Verified Answer (523 Votes)

07/14/2009 20:24:03

Try explode:

$myString = "9,[email protected],8";
$myArray = explode(',', $myString);
print_r($myArray);

Output :

Array
(
    [0] => 9
    [1] => [email protected]
    [2] => 8
)
523

Answer #2 (57 Votes)

09/07/2015 15:27:28
$myString = "9,[email protected],8";
$myArray = explode(',', $myString);
foreach($myArray as $my_Array){
    echo $my_Array.'<br>';  
}

Output

9
[email protected]
8
57

Answer #3 (33 Votes)

07/14/2009 20:24:11
$string = '9,[email protected],8';
$array = explode(',', $string);

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

33
3
Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.