PHP explode

explode() split a string into an array of substrings based on the specified separator.
   array explode(str separator, str string [, int limit])

<?PHP
	$str="The best time is no time";
	$arr=explode(" ",$str);
	foreach($arr as $i) echo "$i, ";  //The, best, time, is, no, time, 
?>

The separator can also be a string.

<?PHP
	$str="The best time is no time";
	$arr=explode("time",$str);
	foreach($arr as $i) echo "$i, ";  //The best , is no , , 
?>

If the returned array element number n is specified, then the return array will be consisted of the first n-1 elements, and all the remaining elements will be combined into the last element.

<?PHP
	$str="The best time is no time";
	$arr=explode(" ",$str,3);
	foreach($arr as $i) echo "$i, ";  //The, best, time is no time, 
?>

If the specified return element number n < 0, then return all other elements except the last n elements.

<?PHP
	$str="The best time is no time";
	$arr=explode(" ",$str,-1);
	foreach($arr as $i) echo "$i, ";  //The, best, time, is, no, 
?>

implode(sep,array) will combine all elements of the array into a string.

<?PHP
	$arr=array("php","js","python");
	$str=implode(" + ", $arr);
	echo "$str";  //php + js + python
?>

if you want to use regular expression as the separator, you can use preg_split() function to do that. preg_split() is much slower. It also has different parameters to adjust the returned results.

<?PHP
	$str="<td>One<td>two<td>three<td>four";
	$arr=preg_split('/\<td\>/',$str);
	foreach($arr as $element) echo "$element, "; //, One, two, three, four, 
?>

explode() may change the special letters to HTML values. htmlspecialchars_decode() function can do the opposite.

<?PHP
$str=htmlspecialchars('<table><tr><td>name<td>address</table>');
echo $str; 
//&lt;table&gt;&lt;tr&gt;&lt;td&gt;name&lt;td&gt;address&lt;/table&gt;
$str2 = htmlspecialchars_decode($str);
echo $str2;
//<table><tr><td>name<td>address</table>
?>

split() function can do a similar job for splitting a string into an array. It can use regex pattern as separator. However it is deprecated and removed after PHP 7.0.


:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • str_replace • strcmp
 • strpos • strrev
 • strrpos • strtr
 • substr • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expression • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2020  Terms of Use | Home