' ) { $ch = $html[++$i]; // advance one char } $tagname = strtolower( substr( $html, $mark, $i-$mark ) ); // Don't advance char after this state. // get the rest of the tag attributes (2.2) while( $ch != '>' ) { $ch = $html[++$i]; // advance one char // special case within quotes // note that this does not handle complex quoting or escapes if ($ch == '"') { while( $ch != '"' ) $ch = $html[++$i]; // advance one char } if ($ch == "'") { while( $ch != "'" ) $ch = $html[++$i]; // advance one char } } $last = $html[$i-1]; // If tag attribute part contains a trailing slash // assume it's self-closing and don't add to tag stack. if ( $last=='/' ) { } // If the tag is an opening tag, put on tagstack. else if ( $closeTag == false ) { // unless it's in the ignoretags array, add it if (!in_array( $tagname, $ignoretags )) $tagstack[] = $tagname; } // If the tag is a closing tag, pop a matching tag off stack // by searching for the first matching tag and removing // that element. else if ( $closeTag == true ) { for($c=count($tagstack)-1; $c >= 0; $c--) { if( $tagstack[$c]==$tagname ) { unset($tagstack[$c]); break; // stop the for loop } } } $ch = $html[++$i]; // advance one char } // main loop // Scan remaining elements, building up a string of closing tags. // Note that string is built up "backwards" to close tags in order // (because the loop reads from the bottom of the stack). foreach( $tagstack as $tag ) $result = ''.$result; return $result; } echo "
";

$test = "