我有一个用随机HTML代码格式化的变量。我把它称为{$text},我将其截断。
该值例如:
如果我截断文本的前30个字母,我会得到这个:
问题是,我无法关闭元素。所以,我需要一个脚本,它检查代码中的元素(其中*可以是任何东西),如果它没有关闭标记,请关闭它们。
请帮帮我。谢谢。
下班后的解决方案,以及4次投票结果@ stackoverflow:
PHP:
...
function closetags($content) {
preg_match_all('##iU', $content, $result);
$openedtags = $result[1];
preg_match_all('#([a-z]+)>#iU', $content, $result);
$closedtags = $result[1];
$len_opened = count($openedtags);
if (count($closedtags) == $len_opened) {
return $content;
}
$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++) {
if (!in_array($openedtags[$i], $closedtags)) {
$content .= ''.$openedtags[$i].'>';
} else {
unset($closedtags[array_search($openedtags[$i], $closedtags)]);
}
}
return $content;
}
...
TPL:
{$pages[j].text|truncate:300|@closetags}
答案
拉出所有打开的标签,将它们逐个推入阵列(array_1)。
拉出所有已关闭的标签,将它们逐个推入阵列(array_2)(这包括自闭标签)。
对于第一个数组(array_1)中第二个数组(array_2)中找不到的标记,将它们添加到html中。
[编辑]
当然,如果你没有写出正确的html,这种方法可能会失败......但是whatchagonnado?
另一种方法是在字符串中向前看以查看哪些标签已关闭并根据需要关闭它们。
另一答案
为了简化,如果代码在截断之前是有效的XML并且你没有将标签切成两半,那么算法将是这样的:
将开口标签推入堆栈
当您找到结束标记时将其弹出(如果代码有效则匹配)
当你到达最后,开始弹出以创建结束。其余标签应附加到原始(截断)文本。
例:
按“div”,“i”,“b”,“span”
找到结束标签“span”
流行“跨度”
找到结束标签“b”
流行“b”
推“div”
截断文本的结尾
Pop“div” - >将
弹出“b” - >将添加到文本中
弹出“我” - >将添加到文本中
Pop“div” - >将
结束
另一答案
对于像我这样的其他人我发现这个代码,我认为这是一个更好的解决方案
添加名为“modifier.html_substr.php”的文件
/*
* Smarty plugin
*
-------------------------------------------------------------
* File: modifier.html_substr.php
* Type: modifier
* Name: html_substr
* Version: 1.0
* Date: June 19th, 2003
* Purpose: Cut a string preserving any tag nesting and matching.
* Install: Drop into the plugin directory.
* Author: Original Javascript Code: Benjamin Lupu
* Translation to PHP & Smarty: Edward Dale
* Modification to add a string: Sebastian Kuhlmann
* Modification to put the added string before closing
or
tags by Peter Carter http://www.podhawk.com-------------------------------------------------------------
*/
function smarty_modifier_html_substr($string, $length, $addstring="")
{
//some nice italics for the add-string
if (!empty($addstring)) $addstring = " " . $addstring . "";
if (strlen($string) > $length) {
if( !empty( $string ) && $length>0 ) {
$isText = true;
$ret = "";
$i = 0;
$currentChar = "";
$lastSpacePosition = -1;
$lastChar = "";
$tagsArray = array();
$currentTag = "";
$tagLevel = 0;
$addstringAdded = false;
$noTagLength = strlen( strip_tags( $string ) );
// Parser loop
for( $j=0; $j
$currentChar = substr( $string, $j, 1 );
$ret .= $currentChar;
// Lesser than event
if( $currentChar == "
// Character handler
if( $isText ) {
// Memorize last space position
if( $currentChar == " " ) { $lastSpacePosition = $j; }
else { $lastChar = $currentChar; }
$i++;
} else {
$currentTag .= $currentChar;
}
// Greater than event
if( $currentChar == ">" ) {
$isText = true;
// Opening tag handler
if( ( strpos( $currentTag, "
( strpos( $currentTag, "/>" ) === FALSE ) &&
( strpos( $currentTag, "") === FALSE ) ) {
// Tag has attribute(s)
if( strpos( $currentTag, " " ) !== FALSE ) {
$currentTag = substr( $currentTag, 1, strpos( $currentTag, " " ) - 1 );
} else {
// Tag doesn't have attribute(s)
$currentTag = substr( $currentTag, 1, -1 );
}
array_push( $tagsArray, $currentTag );
} else if( strpos( $currentTag, "" ) !== FALSE ) {
array_pop( $tagsArray );
}
$currentTag = "";
}
if( $i >= $length) {
break;
}
}
// Cut HTML string at last space position
if( $length < $noTagLength ) {
if( $lastSpacePosition != -1 ) {
$ret = substr( $string, 0, $lastSpacePosition );
} else {
$ret = substr( $string, $j );
}
}
// Close broken XHTML elements
while( sizeof( $tagsArray ) != 0 ) {
$aTag = array_pop( $tagsArray );
// if a
or
tag needs to be closed, put the add-string in firstif (($aTag == "p" || $aTag == "li") && strlen($string) > $length) {
$ret .= $addstring;
$addstringAdded = true;
}
$ret .= "" . $aTag . ">
";
}
} else {
$ret = "";
}
// if we have not added the add-string already
if ( strlen($string) > $length && $addstringAdded == false) {
return( $ret.$addstring );
}
else {
return ( $ret );
}
}
else {
return ( $string );
}
}
?>
用法
{$yourdata|html_substr:300:' ...'}