function close_dangling_tags($html){ #put all opened tags into an array preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU",$html,$result); $openedtags=$result[1]; #put all closed tags into an array preg_match_all("#</([a-z]+)>#iU",$html,$result); $closedtags=$result[1]; $len_opened = count($openedtags); # all tags are closed if(count($closedtags) == $len_opened){ return $html; } $openedtags = array_reverse($openedtags); # close tags for($i=0;$i < $len_opened;$i++) { if (!in_array($openedtags[$i],$closedtags)){ $html .= '</'.$openedtags[$i].'>'; } else { unset($closedtags[array_search($openedtags[$i],$closedtags)]); } } return $html; }
Never been to CodeSnippets before?
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)
Close dangling HTML tags (See related posts)
You need to create an account or log in to post comments to this site.
Related Posts
» Selecting and checking items... in php xhtml html form select option radio checkbox selection persistent
» HTML tag stripper in php html strip strip_tags
» PHP State-Array Generator in php html form dropdown states
» Suexec'ed PHP-FastCGI on Apa... in apache php fastcgi suexec
» My lighttpd php-fastcgi conf... in php fastcgi lighttpd
» Print PHP errors to screen in php debugging
Snippets (source code soon to be available) developed by Peter Cooper and powered by Ruby On Rails