note 67143 deleted from function.preg-match-all by colder

From: Date: Mon, 16 Apr 2007 17:04:28 +0000
Subject: note 67143 deleted from function.preg-match-all by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-125473@lists.php.net to get a copy of this message
Note Submitter: connum at DONOTSPAMME dot googlemail dot com ---- <? 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'); //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]); } $tags=array_reverse($tags); //now this reversion is done again for a better order of close-tags //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; } ?>

« previous php.notes (#125473) next »