Jump to content

cbox


Beginning PHP - Lesson 15 - Explode


fattwam

Recommended Posts

Posted

As you keep advancing in your knowledge of PHP you will find that you need to break-up large strings every now and then - enter the explode() function.

 

explode(); - Splits a string by string (Turns a string into an array made up of the different values.)

 

 

 

 $fullname  = "John Doe";
$name = explode(" ", $fullname);
echo $name[0]; // 'John'
echo $name[1]; // 'Doe'
?>

 

 

This is very useful if you have several different values you need hidden in one string.

 

 

 $list_names  = "name1 name2 name3 name4 name5 name6";
$names = explode(" ", $list_names);
echo $names[0]; // name1
echo $names[1]; // name2
echo $names[2]; // name3
echo $names[3]; // name4
echo $names[4]; // name5
echo $names[5]; // name6
?>

 

 

 

 

_____________________________________________________________________

 

 

Advanced "limit" parameter examples:

 

 

 

 $string = 'one|two|three|four';
print_r(explode('|', $string, 2));      //Notice the "2"
?>

 

 

The above example will output:

 

Array

(

[0] => one

[1] => two|three|four

)

 

 

As you will notice, it only split the $string into 2 pieces and then stopped after the first "|". This is because the number of times it should "explode" the string was set to "2" in the above code. This is just an optional value you can include in the explode command to stop it after a certain number of splits.

 

_____________________________________________________________________

 

 $str = 'one|two|three|four';
print_r(explode('|', $str, -1)); // negative limit (since PHP 5.1)
?>

 

The above example will output:

 

Array

(

[0] => one

[1] => two

[2] => three

)

 

Now it split the $string into three pieces and then left the last value off. This is because the number of times it should "explode" the string was set to "-1", which means it should explode everything in the string but the last "split" value (which is deleted). This is just an optional value you can include in the explode command to stop it after a certain number of splits.

_____________________________________________________________________

 

 

Here is a real-life example of what a line might look like in a file:

2006:David:learnphpfree:what an awsome site!

We can take that and explode it so that we can use the pieces:

 $file_contents = "2006:David:learnphpfree:what an awsome site!";
$contents = explode(':', $file_contents);

echo "Date: $contents[0]";              //prints "Date: 2006"
echo "Name: $contents[1]";              //prints "Name: David"
echo "Website: [url="http://www.$contents"]http://www.$contents[/url][2].com"; //prints "Website: [url="http://www.learnphpfree.com"]http://www.learnphpfree.com[/url]"
echo "Comment: $contents[3]";           //prints "Comment: what an awsome site!"

?>

 

 

 

you can find more information at:

php.net

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...