Home >Backend Development >PHP Tutorial >How to Explode Comma-Delimited Strings into Arrays in PHP?
Exploding Comma-Delimited Strings into Arrays
For many programming tasks, it is necessary to split comma-delimited strings into arrays. This can be accomplished with the explode function.
How to Use explode
The syntax for explode is as follows:
explode(separator, string)
where:
Example
For instance, the following code demonstrates how to split the comma-delimited string "9,[email protected],8" into an array:
$myString = "9,[email protected],8"; $myArray = explode(',', $myString); print_r($myArray);
Output:
Array ( [0] => 9 [1] => [email protected] [2] => 8 )
Beware of CSV Files
It is important to note that if the comma-delimited string comes from a CSV file, it is strongly advised to use the str_getcsv function instead, as it handles CSV-specific issues more effectively.
The above is the detailed content of How to Explode Comma-Delimited Strings into Arrays in PHP?. For more information, please follow other related articles on the PHP Chinese website!