note 27292 deleted from function.strip-tags by vrana
| From: | vrana@php.net | Date: | Mon, 22 Dec 2003 16:23:18 +0000 |
| Subject: | note 27292 deleted from function.strip-tags by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-62334@lists.php.net to get a copy of this message | ||
Note Submitter: etymxris@yahoo.com
----
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 <$tag>"
." without a closing </$tag>.";
}
}
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 <$tag>"
." without a closing </$tag>.";
}
}
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.