1. 首页
  2. WordPress 函数手册

force_balance_tags()

force_balance_tags( string $text )

使用修改过的堆栈平衡字符串的标记。
Balances tags of string using a modified stack.

目录锚点:#参数#返回#源码


参数(Parameters)

参数 类型 必填 说明
$text (string) 必需 文本要平衡。

返回(Return)

(string)平衡文本。


源码(Source)

/**
 * Balances tags of string using a modified stack.
 *
 * @since 2.0.4
 *
 * @author Leonard Lin 
 * @license GPL
 * @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 ) {
	$tagstack = array();
	$stacksize = 0;
	$tagqueue = '';
	$newtext = '';
	// Known single-entity/self-closing tags
	$single_tags = array( 'area', 'base', 'basefont', 'br', 'col', 'command', 'embed', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param', 'source' );
	// Tags that can be immediately nested within themselves
	$nestable_tags = array( 'blockquote', 'div', 'object', 'q', 'span' );

	// WP bug fix for comments - in case you REALLY meant to type '< !--'="" $text="">< !--',="">< !--',="" $text);="" wp="" bug="" fix="" for="" love=""><3 (and="" other="" situations="" with=""><' before="" a="" number)="" $text=""><(&#91;0-9&#93;{1})#', '<$1',="" $text);="" while="" (=""><(\ &#91;w:&#93;*)s*(&#91;^="">]*)>/", $text, $regex) ) {
		$newtext .= $tagqueue;

		$i = strpos($text, $regex[0]);
		$l = strlen($regex[0]);

		// clear the shifter
		$tagqueue = '';
		// Pop or Push
		if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag
			$tag = strtolower(substr($regex[1],1));
			// if too many closing tags
			if ( $stacksize <= 0="" )="" {="" $tag='' ;="" or="" close="" to="" be="" safe="" $tag='/' .="" $tag;="" }="" if="" stacktop="" value="tag" close="" value="" then="" pop="" elseif="" (="" $tagstack&#91;$stacksize="" -="" 1&#93;="=" $tag="" )="" {="" found="" closing="" tag="" $tag=''; // Close Tag
				// Pop
				array_pop( $tagstack );
				$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--) {
							$tagqueue .= '';
							$stacksize--;
						}
						break;
					}
				}
				$tag = '';
			}
		} else { // Begin Tag
			$tag = strtolower($regex[1]);

			// Tag Cleaning

			// If it's an empty tag "<>", do nothing
			if ( '' == $tag ) {
				// do nothing
			}
			// ElseIf it presents itself as a self-closing tag...
			elseif ( substr( $regex[2], -1 ) == '/' ) {
				// ...but it isn't a known single-entity self-closing tag, then don't let it be treated as such and
				// immediately close it with a closing tag (the tag will encapsulate no text as a result)
				if ( ! in_array( $tag, $single_tags ) )
					$regex[2] = trim( substr( $regex[2], 0, -1 ) ) . "> 0 && !in_array($tag, $nestable_tags) && $tagstack[$stacksize - 1] == $tag ) {
					$tagqueue = '';
					$stacksize--;
				}
				$stacksize = array_push( $tagstack, $tag );
			}

			// Attributes
			$attributes = $regex[2];
			if ( ! empty( $attributes ) && $attributes[0] != '>' )
				$attributes = ' ' . $attributes;

			$tag = '<' .="" $tag="" .="" $attributes="" .="" '="">';
			//If already queuing a close tag, then put this tag on, too
			if ( !empty($tagqueue) ) {
				$tagqueue .= $tag;
				$tag = '';
			}
		}
		$newtext .= substr($text, 0, $i) . $tag;
		$text = substr($text, $i + $l);
	}

	// Clear Tag Queue
	$newtext .= $tagqueue;

	// Add Remaining text
	$newtext .= $text;

	// Empty Stack
	while( $x = array_pop($tagstack) )
		$newtext .= ''; // Add remaining tags to close

	// WP fix for the bug with HTML comments
	$newtext = str_replace("<>
更新版本 源码位置 使用 被使用
5.3.0 wp-includes/formatting.php:2463 2 0

force_balance_tags() 为WP2原创文章,链接:https://www.wp2.cn/functions/force_balance_tags/

发表评论

您的电子邮箱地址不会被公开。