变量调节器

一、对变量调节器的介绍

       变量调节器用于变量,自定义函数和字符串。请使用‘|’符号和调节器名称应用调节器。变量调节器由赋予的参数值决定其行为。参数由‘:’符号分开。

二、调节器

      1、capitalize(将变量里的所有单词首字大写)

                 index.php

                      $smarty = new Smarty;

                      $string = "hello world. \n hello everyone!":

                      $smarty->assign("string",$string);

                      $smarty->display('index.tpl');

               index.tpl:

                    {$string}

                   {$string|capitalize}

           输出:

                     hello world. hello everyone!

                     Hello World. Hello Everyone!

2、count_characters(字符计数)

                 index.tpl

                     {$string}总的字符数(不计空格)是:{$string|count_characters}

                     {$string}总的字符数(计空格)是:{$string|count_characters:true}

               输出:

                       hello world. hello everyone! 的字符个数(不计空格):25
                       hello world. hello everyone! 的字符个数(计空格):31

3、cat(连接字符)     

                 index.tpl

                      给字符串$string连接***之后是:{$string|cat:"***!"}

              输出:

                      给字符串hello world. hello everyone! 连接***之后是:hello world. hello everyone! ***

4、count_paragraphs(计算段落)

               index.tpl

                  {$string}段落数:{$string|count_paragraphs}

             输出:

                 hello world. hello everyone!段落数:2

5、count_sentences(计算句数)(以“.”为准)

             index.tpl

                    {$string}总句数为:{$string|count_sentences}

                    输出:

                       hello world. hello everyone!总句数是:1

6、count_words(计算词数)(以 “空格”为准)

              index.tpl

                  {$string}总词数为:{$string|count_words}

              输出:

                     hello world. hello everyone!总词数是:4

7、date_format(格式化时间)

            index.tpl

                 当前时间:    {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}

            输出:

                      当前系统时间为:2012-11-12  16:42:19

          [日期转换说明]

             %a - abbreviated weekday name according to the current locale(根据当地格式输出“星期”缩写格式)

            %A - full weekday name according to the current locale (根据当地格式输出“星期”全称格式)

            %b - abbreviated month name according to the current locale (根据当地格式输出“月”缩写格式)

            %B - full month name according to the current locale (根据当地格式输出“月”全称格式)

           %C - century number (the year divided by 100 and truncated to an integer, range 00 to 99)

           %d - day of the month as a decimal number (range 00 to 31)

           %H - hour as a decimal number using a 24-hour clock (range 00 to 23)

           %m - month as a decimal number (range 01 to 12)

           %M - minute as a decimal number

           %S - second as a decimal number

          %y - year as a decimal number without a century (range 00 to 99)

           %Y - year as a decimal number including the century

          %w - day of the week as a decimal, Sunday being 0

          %W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week

 8、default[默认值]     

               {$string|default:"no title"}

9、escape[编码]

          用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码,十六进制美化,或者javascript转码。默认是html转码。

10、indent[缩进]

            有俩个参数:

                   第一个参数是:决定缩进多少个字符

                  第二个参数是:使用什么字符来代替缩进

               <{$string|indent:10:"&nbsp;"}> 

             输出:          hello world.            hello everyone!

11、lower(将变量字符串小写)

                 $string=Hello WOrld

                  {$string|lower}

             输出: hello world

12、regex_replace(正则替换)

               有俩个参数:

                   第一个参数是:替换正则表达式

                  第二个参数是:用来替换的文本字符

13、replace(替换)

        有俩个参数:

                   第一个参数是:将被替换的文本字串

                  第二个参数是:用来替换的文本字串

              例如:

                   $cap = "hello word! hello everyone":

                    <{$cap|replace:"hello":"hi"}>

                     输出: hi world! hi everyone!   

14、string_format(字符串格式化)

                  $num=123.12345

                      <{$num}>通过%m(m表示整数)d格式化:

                                     <{$num|string_format:"%d"}><br />
                      <{$num}>通过%m.nf()格式化一个实数:

                                     <{$num|string_format:"%6.2f"}>&nbsp;&nbsp;

                                    <{$num|string_format:"%.3f"}><br />
                     <{$num}>通过%e(科学计数法表示)格式化一个实数:

                                    <{$num|string_format:"%e"}>&nbsp;&nbsp;  

                                    <{$num|string_format:"%3.1e"}>

            输出:

                          123.12345通过%m(m表示共有多少字符)d格式化:123
                          123.12345通过%m.nf格式化一个实数:123.12                123.123
                           123.12345通过%e格式化一个实数:1.231235e+2         1.2e+2

15、strip去除(多余空格)             

              用一个空格或一个给定字符替换所有重复空格,换行和制表符.

16、truncate(截取)

Parameter Position参数位置Type参数类型Required必需Default默认Description描述
1integerNo80截取字符的数量
2stringNo...截取后追加在截取词后面的字符串
3booleanNofalse是截取到词的边界(假)还是精确到字符(真)
       
17、upper将变量改为大写

           $smarty->assign('articleTitle', "If Strike isn't Settled Quickly it may Last a While.");

           {$articleTitle|upper}

       输出:

            IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.

18、spacify(插空是一种在字符串的每个字符之间插入空格或其他字符(串))

Parameter Position参数位置Type参数类型Required必需Default默认Description描述
1stringNoone spaceThis what gets inserted between each character of the variable.
将在两个字符之间插入的字符(串)
 实例:

        <{$cap|spacify}>

        <{$cap|spacify:"*"}>

输出:

       h e l l o w o r l d . h e l l o e v e r y o n e ! h e l l o p h p

       h*e*l*l*o* *w*o*r*l*d*.* ** *h*e*l*l*o* *e*v*e*r*y*o*n*e*!* *h*e*l*l*o* *p*h*p

19、组合修改器

       对于同一个变量,你可以使用多个修改器,他们将从左到右按照设定好的顺序被依次组合。使用时必须要用"|"字符作为它们之间的分隔符。

      实例:<{$cap|upper|spacify}>

      输出:H E L L O W O R L D . H E L L O E V E R Y O N E ! H E L L O P H P

 

 

       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值