SP中如何自定义函数可以在SMARTY中使用

#1 xpp1000

我想实现在一个功能就是在页面中显示的文字要我裁剪,只有前多少个字符,函数我已经写好了,现在就是不知道如何注册可以在SMARTY中使用。

2010-11-11 15:42:23

#2 jake

可以试试论坛搜索spaddviewfunction

这里有详细说明:http://speedphp.com/bbs/thread-115-1-1.html

PS:请将此类帖子发往 求助问答 区,谢谢,本贴将转移。

2010-11-11 16:03:22

#3 xpp1000

smarty中有自定义函数:
require(SP_PATH."/SpeedPHP.php");
function utf8Substr($str, $from, $len)
{
    return preg_replace('^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
                       '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*s',
                       '$1',$str);
}
//import('park/utf8.php');
function utf8s($params)

{$str=$params["str"];
$from=$params["from"];
$len=$params["len"];
return preg_replace('^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
                       '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*s',
                       '$1',$str);
}
spAddViewFunction('utf8s','utf8S');  
spRun();
模板:
<{utf8s str=$one.content from=0 len=115}>

2011-01-27 15:41:30