PHP Explode() Function

by Lalita 25-Jul-19

0 305


If you have a string with special characters or symbols and want to split that string then you can use explode function. Explode function is used to break a string into array. It is just contradiction of implode function. It breaks the string into small texts which you define in the separator. 

<?php
$getdata= "Men-Women-Shirts-Kitchen-Books-Formal Dresses";
$final_string= explode('-', $getdata);
print_r ($final_string);
?>

Output:

Array ( [0] => Men [1] => Women [2] => Shirts [3] => Kitchen [4] => Books [5] => Formal Dresses )

explode(separator,string)

Here:

1. separator: is used to split the string into an array.

2. string: is mandatory and used to break into small texts.

You can use different types of strings to split into texts

<?php
$getdata="Men!Women!Shirts!Kitchen!Books!Formal Dresses";
$final_string=explode('!', $getdata);
print_r ($final_string);
?>

Output:

Array ( [0] => Men [1] => Women [2] => Shirts [3] => Kitchen [4] => Books [5] => Formal Dresses )

Now you can use this array using foreach loop.

For example:

foreach($final_string as $get_all){
echo $get_all.'<br>';
}

Output:

Women
Shirts
Kitchen
Books
Formal Dresses


Share:


Comments

    Waiting for your comments

Leave a Reply