force_balance_tags()

WordPress函数补全字符串中的标签

描述

用改进的堆栈自动校正字符串标签。

忽略’use_balanceTags’选项。

用法

<?php force_balance_tags( $text ) ?>

此功能用于简短的帖子摘录列表,以防止不匹配的元素。例如,当 more 标记后的 html 被切断时,它不会 <div><b>This is an excerpt. <!--more--> and this is more text... </b></div> 中断。

参数

$text

(string) (必填) 需要补全标签的文本

默认值: None

返回值

(string) 

补全标签之后的文本

示例

在上面的例子中,<div><b>This is an excerpt. 应改为:<div><b>This is an excerpt. </b></div>通过force_balance_tags函数。

注意

balanceTags() 函数将先判断 ‘use_balanceTags‘ 选项和 $force 参数是否有一个为 true,然后调用 force_balance_tags() 进行补全标签操作。

历史

添加于 版本: 2.0.4

源文件

force_balance_tags() 函数的代码位于 wp-includes/formatting.php.

/**
* Balances tags of string using a modified stack.
*
* @since 2.0.4
*
* @author Leonard Lin <[email protected]>
* @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=””></3><‘ before=”” a=”” number)=”” $text=””></’><([0-9]{1})#’, ‘<$1’,=”” $text);=”” while=”” (=””></([0-9]NO NUMERIC NOISE KEY><( [w:]*)s*([^=””>]*)>/”, $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[$stacksize=“” -=“” 1]=“=” $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(“<><!—“,$newtext);

$newtext = str_replace(“<    !—“,”< !—“,$newtext);

return $newtext;

}

原文:http://codex.wordpress.org/Function_Reference/force_balance_tags

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索