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>

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

技术文章

 

附:

项目结构:

技术文章

文章来自:http://www.cnblogs.com/dee0912/p/4151872.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3