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




split php (5)

Try explode:

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

Output :

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

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

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.


Code:

$string = "9,[email protected],8";

$array  = explode(",", $string);

print_r($array);

$no = 1;
foreach ($array as $line) {
    echo $no . ". " . $line . PHP_EOL;
    $no++;
};

Online:

body, html, iframe { 
  width: 100% ;
  height: 100% ;
  overflow: hidden ;
}
<iframe src="https://ideone.com/pGEAlb" ></iframe>






string