Smarty中常用变量操作符汇总_php实例
本文汇总了Smarty中常用变量操作符,分享给大家供大家参考。具体如下:
php模板引擎smarty的变量操作符可用于操作变量,自定义函数和字符。
语法中使用"|"应用变量操作符,多个参数用":"??指簟?/DIV>
capitalize[首字母大写]
count_characters[计算字符数]
cat[连接字符串]
count_paragraphs[计算段落数]
count_sentences[计算句数]
count_words[计算词数]
date_format[时间格式]
default[默认]
escape[转码]
indent[缩进]
lower[小写 ]
nl2br[换行符替换成
]
regex_replace[正则替换]
replace[替换]
spacify[插空]
string_format[字符串格式化]
strip[去除(多余空格)]
strip_tags[去除html标签]
truncate[截取]
upper[大写]
wordwrap[行宽约束]
组合使用多个操作符
实例如下:
{$title|upper}
{* 取其前40个字符 *}
Topic: {$topic|truncate:40:"..."}
{* 格式化文字串 *}
{"now"|date_format:"%Y/%m/%d"}
{* 在自定义函数里应用调节器 *}
{mailto|upper address="main@cn-web.com"}
capitalize(首字母大写)
index.php页面如下:
$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');
$smarty->display('index.tpl');
index.tpl页面如下:
{$articleTitle|capitalize}
OUTPUT输出如下:
Police Begin Campaign To Rundown Jaywalkers.
count_characters(计算变量里的字符数)
index.php如下:
$smarty->assign('articleTitle', 'Cold Wave Linked to Temperatures.');
$smarty->display('index.tpl');
index.tpl页面如下:
{$articleTitle|count_characters}
OUTPUT输出如下:
Cold Wave Linked to Temperatures.
cat(连接字符串)
将cat里的值连接到给定的变量后面
index.php如下:
$smarty->assign('articleTitle', 'Psychics predict world didn't end');
$smarty->display('index.tpl');
index.tpl页面如下:
OUTPUT输出如下:
count_paragraphs(计算段数)
计算变量里的段落数量
index.php如下:
$smarty->assign('articleTitle', 'War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.');
$smarty->display('index.tpl');
index.tpl模板页面如下:
{$articleTitle|count_paragraphs}
OUTPUT输出如下:
Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.
2
count_sentences(计算句数)
计算变量里句子的数量
index.php如下:
$smarty->assign('articleTitle', 'Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.');
$smarty->display('index.tpl');
index.tpl模板如下:
{$articleTitle|count_sentences}
OUTPUT输出如下:
2
count_words(计算词数)
计算变量里的词数
index.php如下:
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
index.tpl模板如下:
{$articleTitle|count_words}
OUTPUT输出如下:
7
date_format(日期格式)
Parameter Position
参数位置 Type Required Default Description
1 string No %b %e, %Y This is the format for the outputted date.
输出字串的格式
2 string No n/a This is the default date if the input is empty.
输入为空时的默认设置
在给定的函数serftime();里格式日期和时间.
Unix或者mysql等的时间戳(parsable by strtotime)都可以传递到smarty.
设计者可以使用date_format完全控制日期格式.
如果传给date_format的数据是空的,将使用第二个参数作为时间格式
index.php如下:
$smarty->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');
index.tpl:
{$smarty.now|date_format:"%A, %B %e, %Y"}
{$smarty.now|date_format:"%H:%M:%S"}
{$yesterday|date_format}
{$yesterday|date_format:"%A, %B %e, %Y"}
{$yesterday|date_format:"%H:%M:%S"}
OUTPUT输出如下:
Tuesday, February 6, 2001
14:33:00
Feb 5, 2001
Monday, February 5, 2001
14:33:00
default(默认)
Parameter Position Type Required Default Description
1 string No empty This is the default value to output if the variable is empty.
这是变量为空的时候的默认输出
为空变量设置一个默认值.
当变量为空或者未分配的时候,将由给定的默认值替代输出.
index.php如下:
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
index.tpl模板:
{$myTitle|default:"no title"}
OUTPUT输出:
no title
escape(转码)
Parameter Position Type Required Possible Values Default Description
1 string No html,htmlall,url,quotes,hex,hexentity,javascript html This is the escape format to use.
用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码,十六进制美化,或者javascript转码.
默认是html转码
index.php如下:
$smarty->assign('articleTitle', "'Stiff Opposition Expected to Casketless Funeral Plan'");
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle|escape}
{$articleTitle|escape:"html"} {* escapes & " ' *}
{$articleTitle|escape:"htmlall"} {* escapes ALL html entities *}
{$articleTitle|escape:"url"}
{$articleTitle|escape:"quotes"}
href="{$EmailAddress|escape:"hexentity"}mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}
OUTPUT输出:
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan'
'Stiff+Opposition+Expected+to+Casketless+Funeral+Plan'
'Stiff Opposition Expected to Casketless Funeral Plan'
href="bob@me.netmailto:%62%6f%62%40%6d%65%2e%6e%65%74">bob@me.net
indent(缩进)
Parameter Position Type Required Default Description
1 integer No 4 This determines how many characters to indent to.
2 string No (one space) This is the character used to indent with.
在每行缩进字符串,默认是4个字符(pear标准也是).
作为可选参数,你可以指定缩进字符数.
作为第二个可选参数,你可以指定缩进用什么字符代替
index.php如下:
$smarty->assign('articleTitle', 'NJ judge to rule on nude beach.');
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle|indent}
{$articleTitle|indent:10}
{$articleTitle|indent:1:"t"}
OUTPUT输出:
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.
lower(小写)
将变量字符串小写
index.php如下:
$smarty->assign('articleTitle', 'Two Convicts Evade Noose, Jury Hung.');
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle|lower}
OUTPUT输出:
two convicts evade noose, jury hung.
nl2br(换行符替换成
)
所有的换行符将被替换成
.同php的nl2br()函数一样.
index.php如下:
$smarty->assign('articleTitle', "Sun or rain expectedntoday, dark tonight");
$smarty->display('index.tpl');
index.tpl模板:
OUTPUT输出:
today, dark tonight
regex_replace(正则替换)
寻找和替换正则表达式 .
Parameter Position Type Required Default Description
1 string Yes n/a This is the regular expression to be replaced.
替换正则表达式.
2 string Yes n/a This is the string of text to replace with.
使用什么文本字串来替换
index.php如下:
$smarty->assign('articleTitle', "Infertility unlikely tonbe passed on, experts say.");
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle}
{$articleTitle|regex_replace:"/[rtn]/":" "}
OUTPUT输出:
be passed on, experts say.
Infertility unlikely to be passed on, experts say.
replace(替换)
简单的搜索和替换字符串
Parameter Position Type Required Default Description
1 string Yes n/a This is the string of text to be replaced.
将被替换的字符串
2 string Yes n/a This is the string of text to replace with.
用来替换的文本
index.php如下:
$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}
OUTPUT输出:
Child's Stool Great for Use in Vineyard.
Child's Stool Great for Use in Garden.
spacify
是一种在字符串的每个字符之间插入空格或者插入其他的字符(串).
index.php如下:
$smarty->assign('articleTitle', 'Something Went Wrong in Jet Crash, Experts Say.');
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}
OUTPUT输出:
S o m e t h i n g W e n t W r o n g i n J e t C r a s h , E x p e r t s S a y .
S^^o^^m^^e^^t^^h^^i^^n^^g^^ ^^W^^e^^n^^t^^ ^^W^^r^^o^^n^^g^^ ^^i^^n^^ ^^J^^e^^t^^ ^^C^^r^^a^^s^^h^^,^^ ^^E^^x^^p^^e^^r^^t^^s^^ ^^S^^a^^y^^.
string_format(字符串格式化)
Parameter Position Type Required Default Description
1 string Yes n/a This is what format to use. (sprintf)
使用的格式化方式
是一种格式化浮点数的方法.例如十进制数.使用sprintf语法格式化
index.php如下:
$smarty->assign('number', 23.5787446);
$smarty->display('index.tpl');
index.tpl模板:
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}
OUTPUT输出:
23.58
24
strip(去除(多余空格)
替换所有重复的空格,换行和tab为单个.
index.php如下:
$smarty->assign('articleTitle', "Grandmother ofneight makest hole in one.");
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle|strip}
{$articleTitle|strip:" "}
OUTPUT输出:
eight makes hole in one.
Grandmother of eight makes hole in one.
Grandmother of eight makes hole in one.
strip_tags(去除html标签)
去除在之间的所有标签,包括.
index.php如下:
$smarty->assign('articleTitle', "Blind Woman Gets New Kidney from Dad she Hasn't Seen in years.");
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle|strip_tags}
OUTPUT输出:
Blind Woman Gets New Kidney from Dad she Hasn't Seen in years.
truncate(截取)
Parameter Position Type Required Default Description
1 integer No 80 This determines how many characters to truncate to.
指定截取多少字符
2 string No ... This is the text to append if truncation occurs.
截取后加在截取词后的字符串
3 boolean No false This determines whether or not to truncate at a word boundary (false), or at the exact character (true).
检查是否截取到词的边界
截取字符串开始的一段.默认是80个.
你可以指定第二个参数作为在截取的那段字符串后加上什么字符.
默认情况下,smarty会截取到一个词的末尾,
如果你想要精确的截取多少个字符,把第三个参数改为"true"
index.php如下:
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
OUTPUT输出:
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after...
Two Sisters Reunite after
Two Sisters Reunite after---
Two Sisters Reunite after Eigh
Two Sisters Reunite after E...
upper(大写 )
将变量改为大写
index.php如下:
$smarty->assign('articleTitle', "If Strike isn't Settled Quickly it may Last a While.");
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle|upper}
OUTPUT输出:
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.
wordwrap(行宽约束)
可以指定段落的宽度(也就是多少个字符一行,超过这个字符数换行).默认80.
第二个参数可选,可以指定在约束点使用什么字符(默认是换行符n).
默认情况下smarty将截取到词尾,你也可以指定精确截取多少个字符
Parameter Position Type Required Default Description
1 integer No 80 This determines how many columns to wrap to.
指 定段落(句子)的宽度
2 string No n This is the string used to wrap words with.
使用什么字符约束
3 boolean No false This determines whether or not to wrap at a word boundary (false), or at the exact character (true).
是否精确约束到字符
index.php如下:
$smarty->assign('articleTitle', "Blind woman gets new kidney from dad she hasn't seen in years.");
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle|wordwrap:30}
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:"
n"}
{$articleTitle|wordwrap:30:"n":true}
OUTPUT输出:
Blind woman gets new kidney
from dad she hasn't seen in
years.
Blind woman gets new
kidney from dad she
hasn't seen in
years.
Blind woman gets new kidney
from dad she hasn't seen in years.
Blind woman gets new kidney fr
om dad she hasn't seen in year
s.
组合使用多个操作符
可以在一个变量上应用操作符,它们将从左到右依次组合应用.多个操作符必须用"|"符号分开.
index.php页面如下:
$smarty->assign('articleTitle', 'Smokers are Productive, but Death Cuts Efficiency.');
$smarty->display('index.tpl');
index.tpl模板:
{$articleTitle|upper|spacify}
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:30|spacify}
{$articleTitle|lower|spacify|truncate:30:". . ."}
OUTPUT输出:
S M O K E R S A R E P R O D U C T I V E , B U T D E A T H C U T S E F F I C I E N C Y .
s m o k e r s a r e p r o d u c t i v e , b u t d e a t h c u t s...
s m o k e r s a r e p r o d u c t i v e , b u t . . .
s m o k e r s a r e p. . .
希望本文所述对大家的PHP程序设计有所帮助。

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

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

Dreamweaver CS6
視覺化網頁開發工具

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

環境變數是運行應用程式和程式的位置路徑(或環境)。它們可以由使用者建立、編輯、管理或刪除,並在管理某些進程的行為時派上用場。以下介紹如何建立設定檔以同時管理多個變量,而無需在Windows上單獨編輯它們。如何在環境變數中使用設定檔Windows11和10在Windows上,有兩組環境變數–使用者變數(應用於目前使用者)和系統變數(全域應用)。但是,使用像PowerToys這樣的工具,您可以建立一個單獨的設定檔來新增的和現有的變數並一次管理它們。方法如下:步驟1:安裝PowerToysPowerTo

PHP7中引入了嚴格模式,該模式可以幫助開發者減少潛在的錯誤。本文將介紹什麼是嚴格模式以及如何在PHP7中使用嚴格模式來減少錯誤。同時,將透過程式碼範例演示嚴格模式的應用。一、什麼是嚴格模式?嚴格模式是PHP7中的一個特性,它可以幫助開發者編寫更規範的程式碼,減少一些常見的錯誤。在嚴格模式下,會對變數的宣告、型別檢查、函數呼叫等進行嚴格的限制和偵測。通

探索Canvas框架:了解常用的Canvas框架有哪些,需要具體程式碼範例引言:Canvas是HTML5中提供的一個繪圖API,透過它我們可以實現豐富的圖形和動畫效果。為了提高繪圖的效率和便利性,許多開發者開發了不同的Canvas框架。本文將介紹一些常用的Canvas框架,並提供具體程式碼範例,以幫助讀者更深入地了解這些框架的使用方法。一、EaselJS框架Ea

使用Ajax從PHP方法取得變數是Web開發中常見的場景,透過Ajax可以實作頁面無需刷新即可動態取得資料。在本文中,將介紹如何使用Ajax從PHP方法中取得變量,並提供具體的程式碼範例。首先,我們需要寫一個PHP檔案來處理Ajax請求,並傳回所需的變數。下面是一個簡單的PHP檔案getData.php的範例程式碼:

python憑藉其簡單易讀的語法,廣泛應用於廣泛的領域。掌握Python語法的基礎架構至關重要,既可以提高程式效率,又能深入理解程式碼的運作方式。為此,本文提供了一個全面的心智圖,詳細闡述了Python語法的各個面向。變數和資料類型變數是Python中用於儲存資料的容器。心智圖展示了常見的Python資料類型,包括整數、浮點數、字串、布林值和列表。每個資料類型都有其自身的特性和操作方法。運算符運算符用於對資料類型執行各種操作。心智圖涵蓋了Python中的不同運算子類型,例如算術運算子、比

Java中的實例變數是指定義在類別中,而不是方法或建構子中的變數。實例變數也稱為成員變量,每個類別的實例都有自己的一份實例變數副本。實例變數在創建物件的過程中被初始化,以及在物件的生命週期中保存並保持其狀態。實例變數的定義通常放在類別的頂部,可以用任何存取修飾符來聲明,可以是public、private、protected或預設存取修飾符。這取決於我們希望這個變

Spring是一個開源框架,提供了許多註解來簡化和增強Java開發。本文將詳細解釋常用的Spring註解,並提供具體的程式碼範例。 @Autowired:自動組裝@Autowired註解可以用於自動組裝Spring容器中的Bean。當我們在需要依賴的地方使用@Autowired註解時,Spring將會在容器中尋找匹配的Bean並自動注入。範例程式碼如下:@Auto

C中const的詳解及程式碼範例在C語言中,const關鍵字用來定義常數,表示該變數的值在程式執行過程中不能被修改。 const關鍵字可以用來修飾變數、函數參數、函數傳回值。本文將對C語言中const關鍵字的使用進行詳細解析,並提供具體的程式碼範例。 const修飾變數當const用來修飾變數時,表示變數為唯讀變量,一旦賦值就無法再修改。例如:constint
