note 40131 deleted from function.strip-tags by nlopess

From: Date: Thu, 11 Mar 2004 14:52:08 +0000
Subject: note 40131 deleted from function.strip-tags by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-66638@lists.php.net to get a copy of this message
Note Submitter: fil (at) rezox (dot) com - www.RezoX.com ---- /*************************************** ** MatchUpTags Function ** -------------------- ** Written by Fil Laborde - email: fil (at) rezox (dot) com ** ** More thoroughly documented function at: ** http://www.rezox.com/tools/matchuptags.txt **************************************** ** ** FUNCTION: ** Find all the tags in the HTML: ** 1) Then close all the tags that are unclosed (in order of shortest tag to longest) ** 2) Remove closing tags that have no matching openner tag ** ** METHOD: ** We do a shortcut to prevent having to do too many loops (which is slow), by adding ** length to each tag, and then appending the tag count at the end (which allows the ** array_diff() function to work and yet distinguish between all tags) ** ** We order them from shortest to largest tag, and then alphabetically to guestimate the ** closing order of importance (so that things like </td></tr></table> would be ordered ** correctly ** We create a duplicate lowercase version of the page, so that for strpos searches ** we can search for lowercase tags, and then do the modification in the mirrored normal ** HTML page ** ***************************************/ function MatchUpTags( $HTML ){ $HTML_Lowercase = strtolower($HTML); $OpenArray = array(); $CloseArray = array(); $OpenCount = array(); $CloseCount = array(); preg_match_all( "'<([^ |^>]+)( [^>]+)?>'s",$HTML_Lowercase,$TagArray,PREG_PATTERN_ORDER ); foreach( $TagArray[1] AS $Tag ){ if( $Tag[0]=='/' ){ $Tag = substr($Tag,1); #strip the first character '/' $TagLen = strlen($Tag); $CloseCount[$Tag]+=1; $CloseArray[] = str_pad($TagLen,3,'0', STR_PAD_LEFT).' '.$Tag.'_'.$CloseCount[$Tag]; } else { # open tags $TagLen = strlen($Tag); $OpenCount[$Tag]+=1; $OpenArray[] = str_pad($TagLen,3,'0', STR_PAD_LEFT).' '.$Tag.'_'.$OpenCount[$Tag]; }; }; sort($OpenArray,SORT_STRING); $MissingClose = array_diff($OpenArray,$CloseArray); $MissingOpen = array_diff($CloseArray,$OpenArray); $TagAppend = ''; foreach ($MissingClose AS $Tag ) $TagAppend .= '</'.substr($Tag,4,strrpos($Tag,'_')-4).'>'; $HTML .= $TagAppend; foreach( $MissingOpen AS $Tag ){ $TagCnt = intval(substr($Tag,strrpos($Tag,'_')+1)); $Tag = '</'.substr($Tag,4,strrpos($Tag,'_')-4).'>'; #reconstruct the closing tag $TagLen = strlen($Tag); $Pos = -1; for( $i=0; $i<$TagCnt;$i++ ) # with Pos=-1, we start with Pos=0, and loop through each find $Pos = strpos($HTML_Lowercase,$Tag,$Pos+1); if( $Pos!==false ) $HTML = substr_replace( $HTML,str_repeat(' ',$TagLen),$Pos,$TagLen ); }; return $HTML; }; ** EXAMPLE ** $OrigHTML = "<b>sfsfdsdfsdf</b><font size=3><table></ta</tableble>fdsdff<table width='sdfsd'><tr><TD width='sdffd'>sdfsdffsd</td><div class='text'>sdfsdfsdfsdf<span group='dfssdsdf'>fsdsfsdf</div></div><strike style='sdfsd'>ssdfsdfsf</font></b>"; $HTML = MatchUpTags($OrigHTML); ============== EXAMPLE OUTPUT: ============== Original HTML: <b>sfsfdsdfsdf</b><font size=3><table></ta</tableble>fdsdff<table width='sdfsd'><tr><TD width='sdffd'>sdfsdffsd</td><div class='text'>sdfsdfsdfsdf<span group='dfssdsdf'>fsdsfsdf</div></div><strike style='sdfsd'>ssdfsdfsf</font></b> Cleaned up HTML, with appended missing closing tags & removed excess closing tags: <b>sfsfdsdfsdf</b><font size=3><table> fdsdff<table width='sdfsd'><tr><TD width='sdffd'>sdfsdffsd</td><div class='text'>sdfsdfsdfsdf<span group='dfssdsdf'>fsdsfsdf</div> <strike style='sdfsd'>ssdfsdfsf</font> </tr></span></table></table></strike>

« previous php.notes (#66638) next »