note 91346 deleted from function.strip-tags by aharvey
| From: | aharvey@php.net | Date: | Thu, 20 Sep 2012 12:11:37 +0000 |
| Subject: | note 91346 deleted from function.strip-tags by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190918@lists.php.net to get a copy of this message | ||
Note Submitter: magus at otserv dot com dot br
----
Here's a function that verify if in a string have broken HTML tags, returning true if do.
Useful for previning broken tags to affect the page.
<?php
# Example usage: broken_tags("<b>This is a string</u>") returns TRUE,
broken_tags("<b>This is a string</b>") returns FALSE.
function broken_tags($str)
{
preg_match_all("/(<\w+)(?:.){0,}?>/", $str, $v1);
preg_match_all("/<\/\w+>/", $str, $v2);
$open = array_map('strtolower', $v1[1]);
$closed = array_map('strtolower', $v2[0]);
foreach ($open as $tag)
{
$end_tag = preg_replace("/<(.*)/", "</$1>", $tag);
if (!in_array($end_tag, $closed)) return true;
unset($closed[array_search($end_tag, $closed)]);
}
return false;
}
?>