Php Explode Function

PHP 5 Converting Arrays into Strings |pTutorial

Many times you want to perform manipulation on data but the manipulation require different format of data.

For example you have line of string and you want to access particular word. PHP allows you to create an array that contains every element in separate array element.

Explode function

You can convert string into the array with the help of explode function in the following format:

$variable_array = explode(“seprator”,any string);

Let’s take example to explain the concept of explode

<?php

$variable_string = "Sometimes students simply run out of times.";

$variable_array = explode(" ", $variable_string);

print_r($variable_array);

?>

 

Output

Array ( [0] => Sometimes [1] => students [2] => simply [3] => run [4] => out [5] => of [6] => times. )

Note: The string variable is not affected.

Another example of explode function

<?php

$variable_string = "Sometimes students simply run out of times.";$variable_array = explode(" ", 

$variable_string);foreach($variable_array as $value)
{
echo $value."" ;
}

?>

Output

Sometimes
students
simply
run
out
of
times.

 

Let’s take one more example to elaborate the concept of explode function

<?php

$variable_string = "Sometimes,students,simply,run,out,of,times.";$variable_array = explode(",", 

$variable_string);foreach($variable_array as $value)
{
echo $value."" ;
}

<?php

 

Output

Sometimes
students
simply
run
out
of
times.

$variable_array is a simple array variable, so you can manipulate as simple as array variable. You can also access the particular element of the array, length of array. You can perform all operation that you perform on array.

Example

<?php

$variable_string = "Sometimes students simply run out of times.";

$variable_array = explode(" ", $variable_string);

echo "3rd element of array:"; // simply

echo $variable_array[2];

<?php

Output

3rd element of array:simply

If you want to learn more function of PHP visit :Php function



The following two tabs change content below.
Umar Farooque Khan is Software developer and professional blogger serving Worldwide on freelancer and ODesk platform to show the capability of his skills. He is the founder of pTutorial.com. The main purpose of this site is sharing and exchanging of Knowledge related to the recent technologies.