Home > Programming > Using explode function in php

Using explode function in php

January 17th, 2009

Problem

I have a list of emails separated by comma. This is stored in a string For example

$to = “x1@abc.com,x2@abc.com,x3@abc.com”

I need to read this string, separate them and print them in one line each in php.

Solution

You can use php explode function to achieve the result.

<?php

// Example to show usage of explode function in php

$to = “x1@abc.com,x2@abc.com,x3@abc.com”;
$pieces = explode(“,”, $to);

$x = count($pieces);
while ( $x >=0)
{
echo $pieces[$x];
echo “<br />”;
$x–;
}

?>

Copy paste the code and run it. It will print the things in reverse order

x3@abc.com
x2@abc.com
x1@abc.com

I will leave it as an excercize to you to modify the program to print the array element in normal order.

Programming ,

  1. No comments yet.
  1. No trackbacks yet.