Get support from Xavier Media
It is currently Sun Dec 08, 2013 3:43 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Lesson 15 - Explode
PostPosted: Thu Jul 13, 2006 8:01 pm 

Points:
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] <?php
$fullname = "John Doe";
$name = explode(" ", $fullname);
echo $name[0]; // 'John'
echo $name[1]; // 'Doe'
?>[/php]


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


[php] <?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
?>[/php]




_____________________________________________________________________


Advanced "limit" parameter examples:



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


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] <?php
$str = 'one|two|three|four';
print_r(explode('|', $str, -1)); // negative limit (since PHP 5.1)
?>[/php]

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:
Code:
2006:David:learnphpfree:what an awsome site!

We can take that and explode it so that we can use the pieces:
[php] <?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!"

?>[/php]



you can find more information at:
php.net


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Registered users: No registered users


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron

Portal » Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
[
SEO MOD © 2007 StarTrekGuide ]