用smarty怎么调用html页面,使用SMARTY截断HTML格式的文本

我有一个用随机HTML代码格式化的变量。我把它称为{$text},我将其截断。

该值例如:

Lorem ipsum dolor sit amet, con elit.

如果我截断文本的前30个字母,我会得到这个:

Lorem ipsum dolor sit

问题是,我无法关闭元素。所以,我需要一个脚本,它检查代码中的元素(其中*可以是任何东西),如果它没有关闭标记,请关闭它们。

请帮帮我。谢谢。

下班后的解决方案,以及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并且你没有将标签切成两半,那么算法将是这样的:

将开口标签推入堆栈

当您找到结束标记时将其弹出(如果代码有效则匹配)

当你到达最后,开始弹出以创建结束。其余标签应附加到原始(截断)文本。

例:

Lorem ipsum dolor sit amet

按“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 first

if (($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:' ...'}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值