explode() and implode() string functions are very useful function we can solved biggest task using these two methods.
The explode() function break the string into an array with particular separator/delimiter, it splits the string wherever the delimiter character occurs.
Syntax : explode(separator,string,limit)
Here, separator : will be anything which must be present in string e.g. “ ”, “,” “/” etc.
String: which you want to split into an array
Limit: Greater than 0 – Returns an array with a maximum of limit element(s)
Less than 0 – Returns an array except for the last -limit elements()
0 – Returns an array with one element
Let’s see an example of explode()
<?php
// filename: explode_function.php
echo "<strong>Original Text </strong> : ".$text = "this is text we are learning explode string function, and performing some operations on this";
echo "<br/><pre>";
// default, will beak into all possible split`
print_r(explode(" ",$text));
// zero, it return one length of an array
print_r(explode(" ",$text,0));
// here 3,mean return 3 length of an array, first 2 array will be broken by seperate and rest all in last elements.
print_r(explode(" ",$text,3));
// here -2, mean remove last 2 elements from returned array
print_r(explode(" ",$text,-2));
echo "<pre/>";
?>
Remove string before slash using explode() function of string
<?php
$string="remove all text before / rest of all text will be visible";
$data = explode("/",$string);
echo "<br/><strong> whole text : </strong>".$string;
echo "<br/><strong>After Removed text before '/' : </strong>".end($data);
?>
use of implode() function
The implode() function returns a string from the elements of an array.
<?php
$names = ['Pradip','Ajay','Rahul','Adeep','jai','Jyoti','Priyanka','Madhu','Sonu','Ankit'];
echo "<b>Name List in array format. </b> <pre>";
print_r($names);
echo "<pre/>";
$names_string = implode(", ",$names);
echo " <br/> <b>Name List after implode function applied. </b><br/>";
echo $names_string;
?>