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);
limit:
if limit = positive number then the returned array will content limit number of elelements with last element containing rest of the string.

if limit = negative number , all components except the last -limit is retured.

if limit = zero , then it is same as 1
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.
https://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="https://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 -> https: 
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

Using limit

$str="https://www.plus2net.com/php_tutorial/string-explode.php";
$my_array=explode("/",$str,3);
while (list ($key, $val) = each ($my_array)) { 
echo "$key -> $val <br>"; 
}
Output is here
0 -> https: 
1 -> 
2 -> www.plus2net.com/php_tutorial/string-explode.php
With - negative value of limit
$str="https://www.plus2net.com/php_tutorial/string-explode.php";
$my_array=explode("/",$str,-2);
while (list ($key, $val) = each ($my_array)) { 
echo "$key -> $val <br>"; 
}
Output is here
0 -> https: 
1 -> 
2 -> www.plus2net.com 
Your Rating





PHP String Functions
Manju

18-03-2015

this page is very helpful to me...I am really satisfied with these explanations.. and use of explode function ..I am very thankful.

Post your comments , suggestion , error , requirements etc here .




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer