溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在PHP中使用Smarty內(nèi)置變量調(diào)解器

發(fā)布時間:2021-04-02 16:27:26 來源:億速云 閱讀:151 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在PHP中使用Smarty內(nèi)置變量調(diào)解器,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

1、capitalize

將變量里的所有單詞首字大寫。參數(shù)值 boolean 型決定帶數(shù)字的單詞,首字是否大寫。默認(rèn)不大寫

index.php

$tpl->assign('str', 'hello world wor2ld!!!');
$tpl->display('index.html');

index.html(模板文件)

<{$str|capitalize}>
<{$str|capitalize:true}>

結(jié)果為:Hello World wor2ld!!!、 Hello World Wor2Ld!!!

2、count_characters

計算變量里的字符數(shù),該調(diào)解器默認(rèn)不計算空格(空格、制表符、回車…)只計算字符的個數(shù),并且能很好的支持中文字符計算;如果添加參數(shù) true ,則計算空格。

index.html

<{$str|count_characters}> // 不計算空格
<{$str|count_characters:true}> // 計算空格

結(jié)果為:13、14

3、cat

連接字符串,將cat里的值連接到給定的變量后面。

<{$str|cat:' Happy new year.'}>

結(jié)果為:hello world!!! Happy new year.

4、count_paragraphs

計算段數(shù),計算變量里的段落數(shù)量,完美支持中文段落。

index.php

$str = <<assign('str', $str);
$tpl->display('index.html');

index.html

<{$str|count_paragraphs}>

結(jié)果為:3

5、count_sentences

計算句數(shù),計算變量里句子的數(shù)量。注:只支持英文語句,不支持中文。

index.php

$str = <<assign('str', $str);

index.html

<{$str|count_sentences}>

結(jié)果為:2

6、count_words

計算詞數(shù),計算變量里的詞數(shù)。

index.php

$str = <<assign('str', $str);

index.html

<{$str|count_words}>

結(jié)果為:12

7、date_format

日期格式化,具體參數(shù)很多,這里只舉例中國式日期格式

index.php

$tpl->assign('date', time()); // 傳遞時間戳

index.html

<{$date|date_format:'%Y-%m-%d %H:%M:%S'}>

結(jié)果為:2012-01-26 14:37:22

8、default

默認(rèn),為空變量設(shè)置一個默認(rèn)值,當(dāng)變量為空或者未分配的時候,將由給定的默認(rèn)值替代輸出。

index.php

$tpl->assign('str', ''); // 賦值給空

index.html

<{$str|default:'默認(rèn)輸出...'}>、<{$string|default:'沒有定義,默認(rèn)輸出...'}>

結(jié)果為:默認(rèn)輸出...、沒有定義,默認(rèn)輸出...

9、escape

轉(zhuǎn)碼,用于 html 轉(zhuǎn)碼,url 轉(zhuǎn)碼,在沒有轉(zhuǎn)碼的變量上轉(zhuǎn)換單引號,十六進(jìn)制轉(zhuǎn)碼,十六進(jìn)制美化,或者 javascript 轉(zhuǎn)碼,默認(rèn)是html轉(zhuǎn)碼

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); // javascript

index.html

HTML 轉(zhuǎn)碼:<{$html|escape:"html"}>
URL 轉(zhuǎn)碼:<{$url|escape:"url"}>
JS 轉(zhuǎn)碼:<{$js|escape:"javascript"}>

結(jié)果為:

HTML 轉(zhuǎn)碼:Google
URL 轉(zhuǎn)碼:http%3A%2F%2Fwww.google.com.hk
JS 轉(zhuǎn)碼:

10、indent

縮進(jìn),每行縮進(jìn)字符串,第一個參數(shù)指定縮進(jìn)多少個字符串,默認(rèn)是四個字符;第二個參數(shù),指定縮進(jìn)用什么字符代替。

11、lower

小寫,將變量字符串小寫。

使用方法:<{$str|lower}>

12、upper

大寫,將變量改為大寫。

使用方法:<{$str|upper}>

13、nl2br

換行符替換成

所有的換行符將被替換成 ,同php的nl2br()函數(shù)一樣。

14、regex_replace

正則替換,尋找和替換正則表達(dá)式,和 preg_replace() 的語法一樣。

index.php

$tpl->assign('str', 'http://www.google.com');

index.html

<{$str|regex_replace:'/go{2}gle/':'baidu'}>

結(jié)果為:http://www.baidu.com

15、replace

替換,簡單的搜索和替換字符串。

16、spacify

插空,插空(不知道這個詞是什么意思,顧名思義了^^)是一種在字符串的每個字符之間插入空格或者其他的字符(串)。

index.php

$tpl->assign('str', 'hello world!!!');

index.html

<{$str|spacify:"^^"}>

結(jié)果為:h^^e^^l^^l^^o^^ ^^w^^o^^r^^l^^d^^!^^!^^!

17、string_format

字符串格式化,是一種格式化浮點(diǎn)數(shù)的方法,例如:十進(jìn)制數(shù).使用 sprintf 語法格式化。

index.php

$tpl->assign('num', 23.5787446);

index.html

<{$num|string_format:"%.2f"}>
<{$num|string_format:"%d"}>

結(jié)果為:23.58、23

18、strip

替換所有重復(fù)的空格、換行、tab 為單個

index.php

$tpl->assign('str', "Grandmother of\neight makes\t  hole in one.");

index.html

<{$str|strip:" "}>

結(jié)果為:Grandmother of eight makes hole in one.

源代碼:

Grandmother&nbsp;of&nbsp;eight&nbsp;makes&nbsp;hole&nbsp;in&nbsp;one.

19、strip_tags

去除在<和>之間的所有標(biāo)簽,包括<和>。

index.php

$tpl->assign('str', "Google");

index.html

<{$str|strip_tags}>

結(jié)果為:Google(源代碼也是 Google,去掉了標(biāo)簽和標(biāo)簽)

20、truncate

截取,截取字符串開始的一段.默認(rèn)是80個,你可以指定第二個參數(shù)作為在截取的那段字符串后加上什么字符,默認(rèn)情況下,smarty會截取到一個詞的末尾,如果你想要精確的截取多少個字符,把第三個參數(shù)改為"true" 。

index.php

復(fù)制代碼 代碼如下:

$tpl->assign('str', '從前有座山,山上有座廟。廟里有一個老和尚和一個小和尚...');

index.html

<{$str|truncate:10:'...':true}>

上述就是小編為大家分享的怎么在PHP中使用Smarty內(nèi)置變量調(diào)解器了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI