wordwrap 行宽约束

优质
小牛编辑
126浏览
2023-12-01

Parameter PositionTypeRequiredDefaultDescription
1integerNo80This determines how many columns to wrap to.
指定段落(句子)的宽度
2stringNo\nThis is the string used to wrap words with.
使用什么字符约束
3booleanNofalseThis determines whether or not to wrap at a word boundary (false), or at the exact character (true).
是约束到词的边界(假)还是精确到字符(真)

This wraps a string to a column width, default is 80.
As an optional second parameter, you can specify a string of text to wrap the text to the next line (default is carriage return \n).
By default, wordwrap will attempt to wrap at a word boundary. If you want to cut off at the exact character length, pass the optional third parameter of true.

可以指定段落的宽度(也就是多少个字符一行,超过这个字符数换行).默认80.
第二个参数可选,可以指定在约束点使用什么字符(默认是换行符\n).
默认情况下smarty将截取到词尾,如果想精确到设定长度的字符,请将第三个参数设为ture

Example 5-23. wordwrap
例 5-23.行宽约束

index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Blind woman gets new kidney from dad she hasn't seen in years.");
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|wordwrap:30}
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:"<br>\n"}
{$articleTitle|wordwrap:30:"\n":true}
OUTPUT:
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 years.
Blind woman gets new kidney fr
om dad she hasn't seen in year
s.