在项目的 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>

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

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

 

附:

项目结构:

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

相关文章:

  • 2022-02-09
  • 2021-06-19
  • 2021-10-31
  • 2021-08-31
  • 2021-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-07
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-14
相关资源
相似解决方案