代码改变世界

smarty truncate 截取字符串长度

2012-03-15 09:58  呦菜  阅读(574)  评论(0编辑  收藏  举报

smarty truncate 截取
從字符串开始处截取某长度的字符.默认是80個。
你也可以指定第二個参数作为追加在截取字符串后面的文本字串.该追加字串被计算在截取长度中。
默认情況下,smarty会截取到一个词的末尾。
如果你想要精確的截取多少個字符,把第三個参数改为"true" 。


例 截取:

index.php:

 

$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
$smarty->display('index.tpl');



index.tpl:

 

{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}



输出结果:

 

Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after...
Two Sisters Reunite after
Two Sisters Reunite after---
Two Sisters Reunite after Eigh
Two Sisters Reunite after E...

文章如转载,请注明:转载自:第九街-PHP[http://www.9streets.cn/]
本文链接地址:http://www.9streets.cn/art-php-36.html