<?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_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; } ?>
<?PHP $str=htmlspecialchars('<table><tr><td>name<td>address</table>'); echo $str; //<table><tr><td>name<td>address</table> $str2 = htmlspecialchars_decode($str); echo $str2; //<table><tr><td>name<td>address</table> ?>