我正在构建一个Smarty模板。在检查验证之前,一切都很好。
包含描述的截断,但不会添加结束标记。任何标签添加都无济于事。
截断被称为
{$products_data.PRODUCTS_DESCRIPTION|truncate:300}
描述包含超过300个字符,以p
标记开头和结尾。在truncate切断之后,我只有起始标记<p>
。
有没有办法将截断的HTML标签剪切掉?
答案 0 :(得分:5)
我在一个项目中遇到了同样的问题,发现了一个名叫“html_substr”的聪明的修饰符,由Benjamin Lupu / Edward Dale编写。只需在smarty / libs / plugins目录中添加一个名为“modifier.html_substr.php”的新php文件:
(抱歉 - 我不记得来源,所以我只是在这里发布整个功能):
<?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 );
}
}
?>
用法:
{$products_data.PRODUCTS_DESCRIPTION|html_substr:300:' ...'}
答案 1 :(得分:3)
过滤掉html标签,截断文本,然后放回html标签是相当困难的。我建议您使用strip_tags删除所有html标记,然后截断生成的文本。如果你愿意,你可以将剩下的内容嵌入&lt; p&gt;标记:
{$ products_data.PRODUCTS_DESCRIPTION |用strip_tags |截断:300}