Kod
function autoCloseTags($string) {
// automatically close HTML-Tags
// (usefull e.g. if you want to extract part of a blog entry or news as preview/teaser)
// coded by Constantin Gross <connum at googlemail dot com> / 3rd of June, 2006
// feel free to leave comments or to improve this function!
$donotclose=array('br','img','input', 'hr'); //Tags that are not to be closed
//prepare vars and arrays
$tagstoclose='';
$tags=array();
//put all opened tags into an array
preg_match_all("/<(([A-Z]|[a-z]).*)(( )|(>))/isU",$string,$result);
$openedtags=$result[1];
$openedtags=array_reverse($openedtags); //this is just done so that the order of the closed tags in the end will be better
//put all closed tags into an array
preg_match_all("/<\/(([A-Z]|[a-z]).*)(( )|(>))/isU",$string,$result2);
$closedtags=$result2[1];
//look up which tags still have to be closed and put them in an array
for ($i=0;$i<count($openedtags);$i++) {
if (in_array($openedtags[$i],$closedtags)) { unset($closedtags[array_search($openedtags[$i],$closedtags)]); }
else array_push($tags, $openedtags[$i]);
}
//prepare the close-tags for output
for($x=0;$x<count($tags);$x++) {
$add=strtolower(trim($tags[$x]));
if(!in_array($add,$donotclose)) $tagstoclose.='</'.$add.'>';
}
//and finally
return $tagstoclose;
}
manual sie klania