Smarty : substr a variable

允我心安 提交于 2019-11-30 01:13:56

问题


How can I print the first n characters of a smarty variable, e.g. the first 30 characters of {$var}?


回答1:


You should use truncate modifier:

{$var|truncate:30}

More information here.




回答2:


Now there is a function:

{$var|substr:0:30}

or mb_substr for UTF-8 variables:

{$var|mb_substr:0:30}



回答3:


Regarding to your problem Jojo already gave the correct answer.

You should use truncate modifier:

{$var|truncate:30}

But for usability and seo-reaons it would be better to shorten the text via css with text-overflow property. It allows you to print the whole text out to the client but shorten it to a specific width/length and show ... instead.



来源:https://stackoverflow.com/questions/4325283/smarty-substr-a-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!