我有一个用随机HTML代码格式化的变量。我将其称为{$text}
并将其截断。
该值例如:
<div>Lorem <i>ipsum <b>dolor <span>sit </span>amet</b>, con</i> elit.</div>
如果我截断文本的前30个字母,我会得到这个:
<div>Lorem <i>ipsum <b>dolor <span>sit
问题是,我无法关闭元素。所以,我需要一个脚本,检查代码中的<*>
元素(*可能是任何东西),如果它没有关闭标记,请关闭'em。
请帮帮我。感谢。
下班后解决方案,以及4次投票结果@ stackoverflow:
PHP:
...
function closetags($content) {
preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#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}
答案 0 :(得分:0)
拉出所有打开的标签,将它们逐个推入阵列(array_1)。
拉出所有已关闭的标签,将它们逐个推入阵列(array_2)(包括自闭标签)。
对于第一个数组(array_1)中第二个数组(array_2)中找不到的标记,将它们添加到html中。
<强> [编辑] 强>
当然,如果你没有写出正确的html,这种方法会失败,但是什么呢?
另一种方法是在字符串中向前看以查看哪些标签已关闭并根据需要关闭它们。
答案 1 :(得分:0)
为了简化,如果代码在截断之前是有效的XML并且你没有将标签切成两半,那么算法将是这样的:
示例:
<div>Lorem <i>ipsum <b>dolor <span>sit </span>amet</b><div>
</div>
添加到文字</b>
添加到文字</i>
添加到文字</div>
添加到文字答案 2 :(得分:0)
对于像我这样的其他人我发现了这段代码,我认为这是一个更好的解决方案
添加名为&#34; modifier.html_substr.php&#34;
的文件 <?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 <hide@address.com>
* Translation to PHP & Smarty: Edward Dale <hide@address.com>
* Modification to add a string: Sebastian Kuhlmann <hide@address.com>
* Modification to put the added string before closing <p> or <li> 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 = "<i> " . $addstring . "</i>";
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<strlen( $string ); $j++ ) {
$currentChar = substr( $string, $j, 1 );
$ret .= $currentChar;
// Lesser than event
if( $currentChar == "<") $isText = false;
// 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, "<" ) !== FALSE ) &&
( 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 <p> or <li> tag needs to be closed, put the add-string in first
if (($aTag == "p" || $aTag == "li") && strlen($string) > $length) {
$ret .= $addstring;
$addstringAdded = true;
}
$ret .= "</" . $aTag . ">\n";
}
} 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:' ...'}