wordwrap

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)。預設狀況下函式會根據單字作 包裝而不是字元,如果你希望在確定字元長度下作包裝的話,請使用第三個參數並將其設定為 true。

Example 5-22. wordwrap

<?php
    
$smarty
= new Smarty;
$smarty->assign('articleTitle', "Blind woman gets new kidney from dad she hasn't seen in years.");
$smarty->display('index.tpl');

?>

where index.tpl is:

其中 index.tpl 內容為:

{$articleTitle}

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

This will 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<br />
years.

Blind woman gets new kidney
from dad she hasn't seen in
years.