Home > Article > Backend Development > smarty now displays various writing methods of word count_PHP tutorial
Is there a function to limit the number of displayed characters?
have! Use truncate
Cut a certain length of characters from the beginning of the string. The default is 80.
You can also specify the second argument as a text string to be appended to the intercept string. The appended string is calculated into the intercept length.
By default, smarty will truncate to the end of a word.
If you want to intercept exactly how many characters, change the third parameter to "true"
CODE:
index.php:
$smarty =new Smarty;
$smarty->assign('articleTitle','Two Sisters Reunite after Eighteen Years at CheckoutCounter.');
$smarty->display('index.tpl');
www.2cto.com
index.tpl:
{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
OUTPUT:
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...