Skip to content

wordwrap

wordwrap 将字符串按指定的列宽进行换行,默认列宽为 80。作为可选的第二个参数,您可以指定一个字符串,在该字符串处将文本换行到下一行,默认为回车符 "\n"。默认情况下,wordwrap 会尝试在单词边界处进行换行。如果您希望在精确的字符长度处截断,可以将可选的第三个参数设置为 TRUE。这相当于 PHP 的 wordwrap() 函数。

基本用法

{$myVar|wordwrap:30}

参数

参数位置 类型 必需 默认值 描述
1 integer 80 指定要换行的列宽。
2 string \n 用于换行单词的字符串。
3 boolean FALSE 决定是在单词边界处换行(FALSE),还是在精确的字符位置处换行(TRUE)。

示例

<?php

$smarty->assign('articleTitle',
                "Blind woman gets new kidney from dad she hasn't seen in years."
               );

模板如下:

{$articleTitle}

{$articleTitle|wordwrap:30}

{$articleTitle|wordwrap:20}

{$articleTitle|wordwrap:30:"<br />\n"}

{$articleTitle|wordwrap:26:"\n":true}

输出结果如下:

Blind woman gets new kidney from dad she hasn't seen in years. Blind woman gets
new kidney from dad she hasn't seen in years. Blind woman gets new kidney from
dad she hasn't seen in years. Blind woman gets new kidney<br />
from dad she hasn't seen in<br />
years. Blind woman gets new kidn ey from dad she hasn't se en in years.

另请参阅 nl2br{textformat}