note 45133 deleted from ref.strings by tomsommer
| From: | tomsommer@php.net | Date: | Wed, 15 Sep 2004 20:46:51 +0000 |
| Subject: | note 45133 deleted from ref.strings by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76783@lists.php.net to get a copy of this message | ||
Note Submitter: Jestempies (at sputnik.pl)
----
Here's a very simple HTML tag closer. It doesn't cope with some situations, like when
'/' or '>' is contained within the opening tag.
function closeHtmlTags($s) {
// parse all tags from string
preg_match_all("$<([a-z]+)[^\/>]*>$", $s, $open);
preg_match_all("$<[\/]([^>]*)>$", $s, $closed);
if(count($open[1])) {
// parse preg output into, ie.:
// array('a'=>1,'div'=>2,'strong'=>1)
$op = array_count_values($open[1]);
$cl = array_count_values($closed[1]);
// for each open tag, substract 1 from
// both open and closed tags
foreach($op as $k=>$v) {
if(isset($cl[$k]) && $cl[$k]) {
$op[$k]--;
$cl[$k]--;
}
}
// for each open tag left in $op
// add the ommited closing tag(s)
$out = NULL;
foreach(array_reverse($op) as $k=>$v)
if($v>0)
$out .= str_repeat("</{$k}>", $v);
Return $s.$out;
}
else
Return $s;
}