夺命雷公狗---Smarty NO:06 设计篇4 变量调节器
{$var|regex_replace:”/[\t\n]/”:””} :正则替换
{$var|replace:”aa”:”bb”} :字符替换
{$var|spacify:”^^”} :插空(在字符与字符之间插空)
{$var|string_format:”%d”} :字符串格式化 %d:格式化为整数 %s :格式化字符串
{$var|strip: “*”} :去除多余的空格
{$var|strip_tags} :去除html标签
{$var|truncate:30:”…”} :截取指定长度的字符(英文)
{$var|wordwrap:30:”<br>”} :行宽约束
demo3.php
<?php require “smarty/Smarty.class.php”; $smarty = new Smarty(); $str = “hello world”; $str2 = “nihao123″; $str3 = “<ul><li>hello ul li</li></ul>”; $smarty -> assign(‘str’,$str); $smarty -> assign(‘str2′,$str2); $smarty -> assign(‘str3′,$str3); $smarty -> display(“demo3.html”);
demo3.html
<!DOCTYPE html> <html> <head> <meta charset=’utf-8′> <title></title> </head> <body> 正则替换:{$str2|regex_replace:”/\d\d\d/”:”***”} <hr/> 普通替换:{$str2|replace:’123′:’php’} <hr/> 字符插空:{$str|spacify:’*’} <hr/> 格式化数据:{$str|string_format:’%s’} <hr/> 去除多余空格:{$str|strip:’*’} <!–空格处用*号来代替–> <hr/> strip_tags取出html标记:{$str3|strip_tags} <hr/> 字符串截取:{$str|truncate:10} <hr/> 行宽约束:{$str|wordwrap:10:'<hr>’} </body> </html>
变量调节器还可以组合应用
基本语法:
{$var|capitalize|truncate:30:”…”|replace:”aa”:”bb”}
答:首先把var变量的首字母大写,截取30个字符长度,并用…代替,替换子串aa为bb