Get Smarty

Donate

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap glasses from Cheapglasses123.com and save up to 80%.

Where to buy prescription glasses? EyewearCanada.com is your best choice!

Advertisement

truncate

截取字符串到指定长度,默认长度是80. 第二个参数可选,指定了截取后代替显示的字符。 截取后的字符长度是截取规定的长度加上第二个参数的字符长度。 默认truncate会尝试按单词进行截取。 如果你希望按字符截取(单词可能会被截断),需要设置第三个参数TRUE

参数顺序 类型 必选参数 默认值 说明
1 integer No 80 截取的长度
2 string No ... 截取后替代显示的字符,该字符长度会被计算到截取长度内。
3 boolean No FALSE 是否按单词截取FALSE,或是按字符截取TRUE
4 boolean No FALSE 当字符截取的长度刚好等于字符本身长度时,是否截取。 FALSE也会截取。 TRUE是不会截取。 注意如果设置为TRUE,单词的边界会被忽略。

Example 5.21. truncate


<?php
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
?>

   

模板:


{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
{$articleTitle|truncate:30:'..':true: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...
Two Sisters Re..ckout Counter.

   

Comments
No comments for this page.

Advertisement