Archive | Contact | Privacy
31st March 2008
Nerd 2.0

Abridged text, but HTML open?

Again it happened that an unclosed HTML tag has destroyed the excerpts from my layout, but the network can indeed always find a solution quickly.

So here is a PHP code snippet of HTML tags closes all open. Although not a pretty solution, but something better has occurred to me not in a hurry too.

function close_tags($html){
// Alle geöffneten Tags in ein Array
preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU", $html, $result);
$openedtags = $result[1];
// Alle geschlossenen Tags in ein Array
preg_match_all("#</([a-z]+)>#iU",$html,$result);
$closedtags = $result[1];
$lenght = count($openedtags);
// Alle Tags geschlossen
if(count($closedtags) == $lenght)
return $html;
$openedtags = array_reverse($openedtags);
// Tags schließen
for($i = 0; $i < $lenght; $i++) {
if (!in_array($openedtags[$i],$closedtags))
$html .= '</'.$openedtags[$i].'>';
else
unset($closedtags[array_search($openedtags[$i],$closedtags)]);
}
return $html;
}

Quelle: textsnippets.com