Smarty中常用变量操作符汇总(3)

NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
 
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
 
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.

lower(小写)
将变量字符串小写
index.php如下:

复制代码 代码如下:

$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Convicts Evade Noose, Jury Hung.');
$smarty->display('index.tpl');


index.tpl模板:

复制代码 代码如下:

{$articleTitle}
{$articleTitle|lower}


OUTPUT输出:

复制代码 代码如下:

Two Convicts Evade Noose, Jury Hung.
two convicts evade noose, jury hung.

nl2br(换行符替换成<br /> )
所有的换行符将被替换成 <br />.同php的nl2br()函数一样.
index.php如下:

复制代码 代码如下:

$smarty = new Smarty;
$smarty->assign('articleTitle', "Sun or rain expectedntoday, dark tonight");
$smarty->display('index.tpl');


index.tpl模板:

复制代码 代码如下:

{$articleTitle|nl2br}


OUTPUT输出:

复制代码 代码如下:

Sun or rain expected<br />today, dark tonight

regex_replace(正则替换)
寻找和替换正则表达式 .
Parameter Position Type Required Default Description
1 string Yes n/a This is the regular expression to be replaced.
替换正则表达式.

2 string Yes n/a This is the string of text to replace with.
使用什么文本字串来替换
index.php如下:

复制代码 代码如下:

$smarty = new Smarty;
$smarty->assign('articleTitle', "Infertility unlikely tonbe passed on, experts say.");
$smarty->display('index.tpl');


index.tpl模板:

复制代码 代码如下:

{* replace each carriage return, tab & new line with a space *}{* 使用空格替换每个回车,tab,和换行符 *}
{$articleTitle}
{$articleTitle|regex_replace:"/[rtn]/":" "}


OUTPUT输出:

复制代码 代码如下:

Infertility unlikely to
be passed on, experts say.
Infertility unlikely to be passed on, experts say.

replace(替换)
简单的搜索和替换字符串
Parameter Position Type Required Default Description
1 string Yes n/a This is the string of text to be replaced.
将被替换的字符串
2 string Yes n/a This is the string of text to replace with.
用来替换的文本
index.php如下:

复制代码 代码如下:

$smarty = new Smarty;
$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");
$smarty->display('index.tpl');


index.tpl模板:

复制代码 代码如下:

{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}


OUTPUT输出:

复制代码 代码如下:

Child's Stool Great for Use in Garden.
Child's Stool Great for Use in Vineyard.
Child's Stool Great for Use in Garden.


spacify
是一种在字符串的每个字符之间插入空格或者插入其他的字符(串).
index.php如下:

复制代码 代码如下:

$smarty = new Smarty;
$smarty->assign('articleTitle', 'Something Went Wrong in Jet Crash, Experts Say.');
$smarty->display('index.tpl');


index.tpl模板:

复制代码 代码如下:

{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}


OUTPUT输出:

复制代码 代码如下:

Something Went Wrong in Jet Crash, Experts Say.
S o m e t h i n g W e n t W r o n g i n J e t C r a s h , E x p e r t s S a y .
S^^o^^m^^e^^t^^h^^i^^n^^g^^ ^^W^^e^^n^^t^^ ^^W^^r^^o^^n^^g^^ ^^i^^n^^ ^^J^^e^^t^^ ^^C^^r^^a^^s^^h^^,^^ ^^E^^x^^p^^e^^r^^t^^s^^ ^^S^^a^^y^^.

string_format(字符串格式化)
Parameter Position Type Required Default Description
1 string Yes n/a This is what format to use. (sprintf)
使用的格式化方式
是一种格式化浮点数的方法.例如十进制数.使用sprintf语法格式化
index.php如下:

复制代码 代码如下:

$smarty = new Smarty;
$smarty->assign('number', 23.5787446);
$smarty->display('index.tpl');


index.tpl模板:

复制代码 代码如下:

{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}


OUTPUT输出:

复制代码 代码如下:

23.5787446
23.58
24

strip(去除(多余空格)
替换所有重复的空格,换行和tab为单个.
index.php如下:

复制代码 代码如下:

$smarty = new Smarty;
$smarty->assign('articleTitle', "Grandmother ofneight makest hole in one.");
$smarty->display('index.tpl');


index.tpl模板:

复制代码 代码如下:

{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:"&nbsp;"}


OUTPUT输出:

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/7a163d58ee2ac25ef543602089d308b0.html