Search code examples
phparraysstringcsvexplode

Split a comma-delimited string into an array?


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

Is there a way to explode a comma-separated string into a flat, indexed array?

Input:

9,[email protected],8

Output:

['9', 'admin@example', '8']  

Solution

  • Community warning: If that string comes from a csv file, use of str_getcsv() instead is strictly advised, as suggested in this answer

    Try explode:

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

    Output :

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