SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP String Functions

explode()

We can break a string and create an array out of it by using a delimiter. Here is its syntax

array explode( delimiter, string-to-break, limit);

We can specify the delimiter and 2nd parameter is the string we want to break and third parameter is optional by which we can fix the number of elements of the array. Let us try some examples. We have used print_r function to display the elements of the array we get out of our explode function.

$str="Welcome to plus2net php tutorial section";
$ar=explode(" ",$str);
print_r($ar);

We have used one space as delimiter. The output is here

Array ( [0] => Welcome [1] => to [2] => plus2net [3] => php [4] => tutorial [5] => section )

We don't want more than three elements to in the array then change the code like this.

$str="Welcome to plus2net php tutorial section";
$ar=explode(" ",$str,3);
print_r($ar);

Output is here

Array ( [0] => Welcome [1] => to [2] => plus2net php tutorial section )

Breaking an email address using explode

We can use explode function to separate userid part and domain part from an email address . Let us try this example.

$str="my_userid@domain-name.com";
$ar=explode("@",$str);
print_r($ar);

We have used @ as delimiter in above code. Here is the output

Array ( [0] => my_userid [1] => domain-name.com )

Learn more on breaking email address here

Breaking the URL of a page

The URL of a page consist of domain name, directory name and file name. These are joined by / to create path to the page we execute. The Path of the url pointing to this page is here.

http://www.plus2net.com/php_tutorial/string-explode.php

Now using explode function we will break the above url to separate domain part, directory and the file name.

Here is the code.

$str="http://www.plus2net.com/php_tutorial/string-explode.php";
$my_array=explode("/",$str);
while (list ($key, $val) = each ($my_array)) {
echo "$key -> $val <br>";
}

The output is here

0 -> http:
1 ->
2 -> www.plus2net.com
3 -> php_tutorial
4 -> string-explode.php

To get a single output we can use like this

echo $my_array[2];

Output is

www.plus2net.com

Be the first to post comment on this article :


Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer