Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Tutorial Mix DNSandIP Miligraf ITexperts Central 

Change Language

Who's online

There are currently 0 users and 7 guests online.

Lesson 15 - Explode

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.)

<?php
$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.

<?php
$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:

<?php
$string 
'one|two|three|four';
print_r(explode('|'$string2));      //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.

_____________________________________________________________________

<?php
$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:
<?php
$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: http://www.$contents[2].com"//prints "Website: http://www.learnphpfree.com"
echo "Comment: $contents[3]";           //prints "Comment: what an awsome site!"
?>

you can find more information at:
php.net


Submitted by David on November 10, 2006 - 7:50pm.
printer friendly version

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati