本文實例講述了PHP模板引擎Smarty內建變數調解器用法。分享給大家供大家參考,具體如下:
Smarty 中的變數調解器相當於函數,其呼叫方式為:透過"|" 後面直接跟調解函數名,如果有參數,得加在":" 後面,多個參數的話,累加即可。
下面為您介紹 Smarty 中內建的變數調解器:
1、capitalize
將變數裡的所有單字首字大寫。參數值 boolean 型決定帶數字的單字,首字是否大寫。預設不大寫
index.php
$tpl->assign('str', 'hello world wor2ld!!!'); $tpl->display('index.html');
index.html(範本檔案)
<{$str|capitalize}> <{$str|capitalize:true}>
結果為:Hello World wor2ld!!!、 Hello World Wor2Ld!!!
2、count_characters
,此調解器預設不計算空格(空格、製表符、回車…)只計算字元的個數,並且能很好的支援中文字元計算;如果新增參數true ,則計算空格。 index.html<{$str|count_characters}> // 不计算空格 <{$str|count_characters:true}> // 计算空格結果為:13、143、cat連接字串,將cat裡的值連接到給定的變數後面。
<{$str|cat:' Happy new year.'}>結果為:hello world!!! Happy new year.4、count_paragraphs計算段數,計算變數裡的段落數量,完美支持中文。 index.php
$str = <<assign('str', $str); $tpl->display('index.html');index.html
<{$str|count_paragraphs}>ten,註:只支援英文語句,不支援中文。 index.php
$str = <<assign('str', $str);index.html
<{$str|count_sentences}>字結果數為:23553字數的數字計算、術語數。 index.php
$str = <<assign('str', $str);index.html
<{$str|count_words}>index.php
$tpl->assign('date', time()); // 传递时间戳index.html
<{$date|date_format:'%Y-%m-%d %H:%M:%S'}>結果為:2012-01-26 14:37:228、default默認,為空變量設置一個默認值,當變數為空或未分配的時候,將由給定的預設值替代輸出。 index.php
$tpl->assign('str', ''); // 赋值给空index.html
<{$str|default:'默认输出...'}>、<{$string|default:'没有定义,默认输出...'}>.於html 轉碼,url 轉碼,在沒有轉碼的變數上轉換單引號,十六進位轉碼,十六進位美化,或javascript 轉碼,預設是html轉碼index.php
$html = <<Google html; $js = << for (var i=0; i<100; i++) { window.alert(i); } js; $tpl->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 转码:Google URL 转码:http%3A%2F%2Fwww.google.com.hk JS 转码:進多少個字符串,預設是四個字元;第二個參數,指定縮排用什麼字元取代。 11、lower小寫,將變數字串小寫。 使用方法:12、upper大寫,將變數改為大寫。 使用方法:13、nl2br換行符替換成所有的換行符將被替換成 ,同php的nl2br()函數一樣。 14、regex_replace正規替換,尋找和替換正規表示式,和 preg_replace() 的語法一樣。 index.php
$tpl->assign('str', 'http://www.google.com');index.html
<{$str|regex_replace:'/go{2}gle/':'baidu'}>。 16、spacify插空,插空(不知道這個字是什麼意思,顧名思義了^^)是一種在字串的每個字元之間插入空格或其他的字元(字串)。 index.php
$tpl->assign('str', 'hello world!!!');index.html
<{$str|spacify:"^^"}>^^
^^^^ ^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、23383.單一
index. php
$tpl->assign('str', "Grandmother of\neight makes\t hole in one.");
index.html
<{$str|strip:" "}>
結果為:Grandmother of eightkes makes
hole in one.19、strip_tags去除在之間的所有標籤,包括。 index.php
$tpl->assign('str', "Google");index.html
<{$str|strip_tags}>.
截取,截取字符串开始的一段.默认是80个,你可以指定第二个参数作为在截取的那段字符串后加上什么字符,默认情况下,smarty会截取到一个词的末尾,如果你想要精确的截取多少个字符,把第三个参数改为"true" 。
index.php
$tpl->assign('str', '从前有座山,山上有座庙。庙里有一个老和尚和一个小和尚...');
index.html
<{$str|truncate:10:'...':true}>
结果为:从前有座山,山...
希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。
更多PHP模板引擎Smarty内置变量调解器用法详解相关文章请关注PHP中文网!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

禪工作室 13.0.1
強大的PHP整合開發環境