ThinkPHP 3.2.2 视图模板中使用字符串截取函数

在项目的 Common/function.php 文件里( 项目结构如图 ) 添加函数:

/*字符串截断函数+省略号*/
function subtext($text, $length)
{
    if(mb_strlen($text, 'utf8') > $length) 
    return mb_substr($text, 0, $length, 'utf8').'...';
    return $text;
}

然后在模板文件中这样写 ( 使用 TP 模板 ),例如在循环中:

<volist name="list" id="vo">
{$vo.comment|subtext=40}
</volist>

前台页面显示 ( 会带... ):

 

附:

项目结构:

posted @ 2014-12-08 20:26  nemo20  阅读(3615)  评论(0)    收藏  举报
访客数:AmazingCounters.com
2016/05/17 起统计