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

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

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.


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.


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

Here is the code.

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


Your Rating

PHP String Functions


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

Post Comment This is for short comments only. Use the forum for more discussions.

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