Revision: 52037
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 11, 2011 22:23 by bavithra
Initial Code
function closetags ( $html ) { $html = strrpos ( $html, "<" ) > strrpos ( $html, ">" ) ? rtrim ( substr ( $html, 0, strrpos ( $html, "<" ) ) ) : rtrim ( $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; } $snippet = "<div>This is some interesting <strong><em>content!"; $x = closetags ( $snippet ); print htmlspecialchars ( $x );
Initial URL
http://snipplr.com/view/3618/close-tags-in-a-htmlsnippet/
Initial Description
Initial Title
Code to close any HTML tags that are not-closed in a string
Initial Tags
Initial Language
PHP