WordPress函数文档force_balance_tags()
补全字符串中的标签 描述 译文 用改进的堆栈自动校正字符串标签。 忽略’use_balanceTags’选项。…
补全字符串中的标签
描述
译文
用改进的堆栈自动校正字符串标签。
忽略’use_balanceTags’选项。
原文
补全字符串中的标签。
忽略 ‘use_balanceTags
‘ 选项。
用法
<?php force_balance_tags( $text ) ?>
This function is used in the short post excerpt list, to prevent unmatched elements.For example, it makes <div><b>This is an excerpt. <!--more--> and this is more text... </b></div>
not break, when the html after the more tag is cut off.
参数
$text
(string) (必填) 需要补全标签的文本
默认值: None
返回值
(string)
补全标签之后的文本
示例
In the example above,
<div><b>This is an excerpt.
should be changed to:
<div><b>This is an excerpt. </b></div>
by the force_balance_tags function.
注意
-
balanceTags() 函数将先判断 ‘
use_balanceTags
‘ 选项和$force
参数是否有一个为true
,然后调用force_balance_tags()
进行补全标签操作。
历史
添加于 版本: 2.0.4
源文件
force_balance_tags() 函数的代码位于 wp-includes/formatting.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* 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;
}
|
=>(>@acm.org>
- 原文:http://codex.wordpress.org/Function_Reference/force_balance_tags
类别:WordPress函数文档、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!