Easy way to parse comma separated item in PHP

Easy way to parse comma separated item in PHP

Welcome Quick tips#2. Last day, in Quick tips#1 we learned How to Get Remote IP Address in PHP. Today We are going to learn about PHP Parsing. Using PHP, we can easily extract the individual elements of a comma-separated list. PHP has a great function named explode() that makes a comma-separated string list into an array of individual list elements. Lets jump in to details-

– Easy way to parse comma separated item in PHP:

Following example clearly illustrates this process that the explode() function scans the string for the delimiter( like- comma(;), at-the-rate(@),star(*) ) and cuts out the pieces around it, placing them in an one dimensional array. Once the list items have been extracted we can use a foreach() loop to show them properly. In result processing system, it’s a great way to parse information using explode function.


// define a comma separated list

$string= "cat,dog,hen,tiger,pigeon,camel";

// decompose string into array 

// using comma as delimiter

$exploded_string= explode(",", $string);
// output:

// print individual elements

foreach ($exploded_string as $key=>$value) {
    echo $value . "\n";
// output:


Was this information useful? What other tips would you like to read about in the future? Share your comments, feedback and experiences with us by commenting below!

Share It

Related Posts