note 27292 added to function.strip-tags

From: Date: Sun, 01 Dec 2002 01:12:13 +0000
Subject: note 27292 added to function.strip-tags
Groups: php.notes 
Request: Send a blank email to php-notes+get-40194@lists.php.net to get a copy of this message
Here is a function to find broken tags. It, like my_htmlspecialchars() above, does not account for the <a> tag. $SPANNING_TAGS = "b|i|u|blockquote|ol|ul"; $NON_SPANNING_TAGS = "li|p"; // must call my_htmlspecialchars() and stripslashes() first! function get_broken_tags($text) { global $SPANNING_TAGS; global $NON_SPANNING_TAGS; $result = $text; $matches = array(); $return_value = array(); $j = 0; // failsafe counter do { preg_match_all("/<($SPANNING_TAGS)>/i", $result, $matches); $match_found = false; for ($i=0; $i<count($matches[0]); $i++) { $tag = $matches[1][$i]; $matches2 = array(); preg_match_all("/(<$tag>(<($NON_SPANNING_TAGS)" .">|[^<>])*<\/$tag>)/i", $result, $matches2); for ($k=0; $k<count($matches2[0]); $k++) { $result = str_replace($matches2[1][$k], "", $result); } if (count($matches2[0]) > 0) { $match_found = true; } } $j++; } while ($match_found && $j < 10); preg_match_all("/<($SPANNING_TAGS)>/i", $result, $matches); if (count($matches[0]) > 0) { for ($i=0; $i<count($matches[0]); $i++) { $tag = $matches[1][$i]; $return_value[] = "You have an opening &lt;$tag&gt;" ." without a closing &lt;/$tag&gt;."; } } preg_match_all("/<\/($SPANNING_TAGS)>/i", $result, $matches); if (count($matches[0]) > 0) { for ($i=0; $i<count($matches[0]); $i++) { $tag = $matches[1][$i]; $return_value[] = "You have an opening &lt;$tag&gt;" ." without a closing &lt;/$tag&gt;."; } } return $return_value; } The return value is an array, one message for each broken tag. The function works quite well, handling nested tags as well. -- http://www.php.net/manual/en/function.strip-tags.php http://master.php.net/manage/user-notes.php?action=edit+27292 http://master.php.net/manage/user-notes.php?action=delete+27292 http://master.php.net/manage/user-notes.php?action=reject+27292

« previous php.notes (#40194) next »