PHP String explode

PHP String explode

Posted in : PHP Posted on : January 7, 2011 at 6:29 PM Comments : [ 0 ]

This section contains the detail about the PHP String explode.

PHP String explode

In PHP, you can break a string into different pieces using explode() function. You can break string where blank spaces" " are available or hyphen"-" or any other special character is used.

You need to pass that special character into explode function to tell it that break the string into pieces where each provided  special character lies.

Example is given below :

$str = "hi-my-name-is-khan"; 

$strParts = explode("-", $str);
echo "Original String = $str <br />";
echo "First part = $strParts[0]<br />";
echo "Second part = $strParts[1]<br />";
echo "Third part = $strParts[2]<br />";
echo "Fourth part = $strParts[3]<br />";
echo "Fifth part = $strParts[4]<br />";

Output :

Original String = hi-my-name-is-khan
First part = hi
Second part = my
Third part = name
Fourth part = is
Fifth part = khan

Setting Limit of breaking string

You can set limit of pieces up to which explode break string into pieces.

Given below example gives you a clear view :

$str = "Welcome to Devmanuals!! Devmanuals Rocks!!"; 

//Setting explode to only four pieces
$strParts = explode(" ", $str, 4);
for($i = 0; $i < count($strParts); $i++){
echo "String Piece $i = $strParts[$i] <br />";

In the above code, you can see that explode has an additional argument 4, which bound the breaking into 4 pieces.

Output :

String Piece 0 = Welcome
String Piece 1 = to
String Piece 2 = Devmanuals!!
String Piece 3 = Devmanuals Rocks!!

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics