/ Published in: PHP
Expand |
Embed | Plain Text
/** * Balances tags of string using a modified stack. * * @since 2.0.4 * * @author Leonard Lin <[email protected]> * @license GPL v2.0 * @copyright November 4, 2001 * @version 1.1 * @todo Make better - change loop condition to $text in 1.2 * @internal Modified by Scott Reilly (coffee2code) 02 Aug 2004 * 1.1 Fixed handling of append/stack pop order of end text * Added Cleaning Hooks * 1.0 First Version * * @param string $text Text to be balanced. * @return string Balanced text. */ function force_balance_tags( $text ) { $nestable_tags = array('blockquote', 'div', 'span'); //Tags that can be immediately nested within themselves # WP bug fix for comments - in case you REALLY meant to type '< !--' # WP bug fix for LOVE <3 (and other situations with '<' before a number) $newtext .= $tagqueue; // clear the shifter $tagqueue = ''; // Pop or Push // if too many closing tags if($stacksize <= 0) { $tag = ''; //or close to be safe $tag = '/' . $tag; } // if stacktop value = tag close value then pop else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag $tag = '</' . $tag . '>'; // Close Tag // Pop $stacksize--; } else { // closing tag not at top, search for it for ($j=$stacksize-1;$j>=0;$j--) { if ($tagstack[$j] == $tag) { // add tag to tagqueue for ($k=$stacksize-1;$k>=$j;$k--){ $stacksize--; } break; } } $tag = ''; } } else { // Begin Tag // Tag Cleaning // If self-closing or '', don't do anything. } // ElseIf it's a known single-entity tag but it doesn't close itself, do so $regex[2] .= '/'; } else { // Push the tag onto the stack // If the top of the stack is the same as the tag we want to push, close previous tag $stacksize--; } } // Attributes $attributes = $regex[2]; if($attributes) { $attributes = ' '.$attributes; } $tag = '<'.$tag.$attributes.'>'; //If already queuing a close tag, then put this tag on, too if ($tagqueue) { $tagqueue .= $tag; $tag = ''; } } } // Clear Tag Queue $newtext .= $tagqueue; // Add Remaining text $newtext .= $text; // Empty Stack $newtext .= '</' . $x . '>'; // Add remaining tags to close } // WP fix for the bug with HTML comments return $newtext; }
You need to login to post a comment.
