note 45133 added to ref.strings
| From: | (atsputnik.pl) | Date: | Fri, 27 Aug 2004 02:22:05 +0000 |
| Subject: | note 45133 added to ref.strings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-75395@lists.php.net to get a copy of this message | ||
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;
}
----
Manual Page -- http://www.php.net/manual/en/ref.strings.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45133
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45133&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45133&report=yes
Search -- http://master.php.net/manage/user-notes.php