PHP preg_split()

preg_split() cuts a string based on a separator which supports regular expression. It is similar to explode() which do not supports regular expression. preg_split() is much slower than explode().

<?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, 
?>

preg_split grammer is preg_split(pattern,string,limit=-1,flags=0). If parameter limit is provided, only substrings upstream the limit position will be cut, and the downstream remaining substring will be the last element of the return array. There are three flags options:
   • PREG_SPLIT_NO_EMPTY: Only non empty elements will be returned
   • PREG_SPLIT_OFFSET_CAPTURE: Returns substrings as well as their offsets
   • PREG_SPLIT_DELIM_CAPTURE: Parenthesized expression in the separator will be returned also

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

Using PREG_SPLIT_DELIM_CAPTURE to get the parenthesized substring.

<?PHP
	$str="<tr>One<td>two<tr>three<td>four";
	$arr=preg_split('/<(t[rd])>/',$str,NULL,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
	foreach($arr as $element) echo "$element "; //tr One td two tr three td four
?>

Using PREG_SPLIT_OFFSET_CAPTURE to get the offset of each substring.

<?PHP
	$str="<td>One<td>two<td>three<td>four";
	$arr=preg_split('/<td>/',$str,NULL,PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_NO_EMPTY);
	foreach($arr as $element)
	{
	   echo "$element[0], $element[1];\n"; //One, 4; two, 11; three, 18; four, 27; 
	}
?>

preg_split() 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, and it can use regex pattern as separator too. 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
 • regular expr. • 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, krsort • 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, filesize
 • mkdir, rmdir • 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 expr. • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2024  | Terms of Use | Privacy | Home