note 93694 added to function.preg-match-all
| From: | avengis at gmail dot com | Date: | Wed, 23 Sep 2009 09:25:32 +0000 |
| Subject: | note 93694 added to function.preg-match-all | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160968@lists.php.net to get a copy of this message | ||
The next function works with almost any complex xml/xhtml string
/**
* Find and close unclosed xml tags
**/
function close_tags($text) {
$patt_open =
"%((?<!</)(?<=<)[\s]*[^/!>\s]+(?=>|[\s]+[^>]*[^/]>)(?!/>))%";
$patt_close = "%((?<=</)([^>]+)(?=>))%";
if (preg_match_all($patt_open,$text,$matches))
{
$m_open = $matches[1];
if(!empty($m_open))
{
preg_match_all($patt_close,$text,$matches2);
$m_close = $matches2[1];
if (count($m_open) > count($m_close))
{
$m_open = array_reverse($m_open);
foreach ($m_close as $tag) $c_tags[$tag]++;
foreach ($m_open as $k => $tag) if ($c_tags[$tag]--<=0)
$text.='</'.$tag.'>';
}
}
}
return $text;
}
----
Server IP: 80.64.47.13
Probable Submitter: 84.89.61.82
----
Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php
Edit -- https://master.php.net/note/edit/93694
Del: integrated -- https://master.php.net/note/delete/93694/integrated
Del: useless -- https://master.php.net/note/delete/93694/useless
Del: bad code -- https://master.php.net/note/delete/93694/bad+code
Del: spam -- https://master.php.net/note/delete/93694/spam
Del: non-english -- https://master.php.net/note/delete/93694/non-english
Del: in docs -- https://master.php.net/note/delete/93694/in+docs
Del: other reasons-- https://master.php.net/note/delete/93694
Reject -- https://master.php.net/note/reject/93694
Search -- https://master.php.net/manage/user-notes.php