smarty 去掉html标签,从Smarty变量中删除HTML标记

simplyray..

5

我在一个项目中遇到了同样的问题,发现了一个名叫"html_substr"的聪明的修饰符,由Benjamin Lupu/Edward Dale编写.只需将一个名为"modifier.html_substr.php"的新php文件添加到smarty/libs/plugins目录中:

(抱歉 - 我不记得来源,所以我只是在这里发布整个功能):

/*

* 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 . ">\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:' ...'}

simpleray,source是[这里](http://www.smarty.net/forums/viewtopic.php?t=533).还有更新的版本. (2认同)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值