$str = <index.html
<{$str|count_words}>結果為:12
7、date_format
日期格式化,具體參數很多,這裡只舉例中國式日期格式
index.php
$tpl->assign('date', time()); // 傳遞時間戳記index.html
<{$date|date_format:'%Y-%m-%d %H:%M:%S'}>結果為:2012-01-26 14:37:22
8、default
預設,為空白變數設定一個預設值,當變數為空白或者未分配的時候,將由給定的預設值替代輸出。
index.php
$tpl->assign('str', ''); // 賦值給空index.html
<{$str|default:'預設輸出...'}>、<{$string|default:'沒有定義,預設輸出...'}>結果為:預設輸出...、沒有定義,預設輸出...
9、escape
轉碼,用於 html 轉碼,url 轉碼,在沒有轉碼的變數上轉換單引號,十六進位轉碼,十六進位美化,或者 javascript 轉碼,預設是html轉碼
index.php
$html = <assign('html', $html); // html$tpl->assign('url', 'http://www.google.com.hk'); // url$tpl->assign('js', $js); // javascriptindex.html
HTML 轉碼:<{$html|escape:"html"}>URL 轉碼:<{$url|escape:"url"}>JS 轉碼:<{$js|escape:"javascript"}>結果為:
HTML 轉碼:GoogleURL 轉碼:http%3A%2F%2Fwww.google.com.hkJS 轉碼:
10、indent
縮排,每行縮排字串,第一個參數指定縮排多少個字串,預設是四個字元;第二個參數,指定縮排用什麼字元代替。
11、lower
小寫,將變數字串小寫。
使用方法:<{$str|lower}>
12、upper
大寫,將變數改為大寫。
使用方法:<{$str|upper}>
13、nl2br
分行符號替換成
所有的分行符號將被替換成 ,同php的nl2br()函數一樣。
14、regex_replace
正則替換,尋找和替換Regex,和 preg_replace() 的文法一樣。
index.php
$tpl->assign('str', 'http://www.google.com');index.html
<{$str|regex_replace:'/go{2}gle/':'baidu'}>結果為:http://www.baidu.com
15、replace
替換,簡單的搜尋和替換字串。
16、spacify
插空,插空(不知道這個詞是什麼意思,顧名思義了^^)是一種在字串的每個字元之間插入空格或者其他的字元(串)。
index.php
$tpl->assign('str', 'hello world!!!');index.html
<{$str|spacify:"^^"}>結果為:h^^e^^l^^l^^o^^ ^^w^^o^^r^^l^^d^^!^^!^^!
17、string_format
字串格式化,是一種格式化浮點數的方法,例如:十進位數.使用 sprintf 文法格式化。
index.php
$tpl->assign('num', 23.5787446);index.html
<{$num|string_format:"%.2f"}><{$num|string_format:"%d"}>結果為:23.58、23
18、strip
替換所有重複的空格、換行、tab 為單個
index.php
$tpl->assign('str', "Grandmother of\neight makes\t hole in one.");index.html
<{$str|strip:" "}>結果為:Grandmother of eight makes hole in one.
原始碼:
Grandmother of eight makes hole in one.
19、strip_tags
去除在<和>之間的所有標籤,包括<和>。
index.php
$tpl->assign('str', "Google");index.html
<{$str|strip_tags}>結果為:Google(原始碼也是 Google,去掉了標籤和標籤)
20、truncate
截取,截取字串開始的一段.預設是80個,你可以指定第二個參數作為在截取的那段字串後加上什麼字元,預設情況下,smarty會截取到一個詞的末尾,如果你想要精確的截取多少個字元,把第三個參數改為"true" 。
index.php
複製代碼 代碼如下:$tpl->assign('str', '從前有座山,山上有座廟。廟裡有一個老和尚和一個小和尚...');
index.html
<{$str|truncate:10:'...':true}>結果為:從前有座山,山...
更多關於PHP相關內容感興趣的讀者可查看本站專題:《smarty模板入門基礎教程》、《PHP模板技術總結》、《PHP基於pdo操作資料庫技巧總結》、《PHP運算與運算子用法總結》、《PHP網路編程技巧總結》、《PHP基本文法入門教程》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家基於smarty模板的PHP程式設計有所協助。
您可能感興趣的文章: