smarty实用的变量操作符
1、sm_operator.php文件内容如下:
<?php
include("smarty_inc.php");
$name="hi,word.We;com to here. <a href='http://google.cn'>google.cn</a>";
$smarty->assign("name",$name);
$date="a122742f4710.34f";
$smarty->assign("date",$date);
$string="hello,smarty. I am XiaoHongyang.";
$smarty->assign("string",$string);
$smarty->display("sm_operator.htm");
?>
2、./templates/sm_operator.htm内容如下:
<html>
<head><title></title></head>
<body>
原始内容:{$string}<HR>
首字母大写capitalize{$string|capitalize}<br> <!-- 首字母大写capitalize -->
字符计数{$string|count_characters}<br> <!-- 字符计数 -->
连接字符{$string|cat:"连接字符"}<br>
段落计数{$string|count_paragraphs}<br>
语句计数{$string|count_sentences}<br>
单词计数{$string|count_words}<br>
时间格式{$date|date_format:'%Y%m%d '}
当前时间{$smarty.now|date_format:'%Y%m%d'}<br>
默认{$default|default:"没有变量"}<br>
转码:{$name|escape:'html '}<br>
缩进:{$string|indent:10:'...'}
小写lover:{$string|lower}<br>
大写lover:{$string|upper}<br>
替换replace:{$name|replace:'hi':'hello!'}
插空spacify:{$name|spacify:_}
字符串格式化:{$date|string_format:'%2f'}<br>
去除多余空格:{$name|strip:''}<br>
在有空格的地方插入内容:{$name|strip:'嘿'}<br>
去除html标签:{$name|strip_tags}<br>
截取truncate:{$name|truncate:7}<br>
行约束wordwrap:{$string|wordwrap:1:"<br>/n" }
</body>
</html>
运行结果如下:
原始内容:hello,smarty. I am XiaoHongyang.
首字母大写capitalizeHello,Smarty. I Am XiaoHongyang.
字符计数29
连接字符hello,smarty. I am XiaoHongyang.连接字符
段落计数1
语句计数2
单词计数4
时间格式20100705 当前时间20100705
默认没有变量
转码:hi,word.We;com to here. google.cn
缩进:..............................hello,smarty. I am XiaoHongyang. 小写lover:hello,smarty. i am xiaohongyang.
大写lover:HELLO,SMARTY. I AM XIAOHONGYANG.
替换replace:hello!,word.We;com to here. google.cn 插空spacify:h_i_,_w_o_r_d_._W_e_;_c_o_m_ _t_o_ _h_e_r_e_._ _ _<_a_ _h_r_e_f_=_'_h_t_t_p_:_/_/_g_o_o_g_l_e_._c_n_'_>_g_o_o_g_l_e_._c_n_<_/_a_> 字符串格式化:0.000000
去除多余空格:hi,word.We;comtohere.