


Summary of commonly used variable operators in Smarty, summary of smarty operators_PHP tutorial
A summary of commonly used variable operators in Smarty, a summary of smarty operators
This article summarizes the commonly used variable operators in Smarty and shares them with you for your reference. The details are as follows:
The variable operator of PHP template engine smarty can be used to operate variables, custom functions and characters.
Use "|" in the syntax to apply the variable operator, and use ":" for multiple parameters?/DIV>
capitalize[capitalize]
count_characters[Count the number of characters]
cat[connection string]
count_paragraphs[Count number of paragraphs]
count_sentences[Count sentences]
count_words[Count the number of words]
date_format[time format]
default[default]
escape[transcoding]
indent[indent]
lower[lowercase]
nl2br[Line breaks replaced with
]
regex_replace[regular replacement]
replace[replace]
spacify[insert blank]
string_format[string format]
strip[remove (excess spaces)]
strip_tags[remove html tags]
truncate[interception]
upper[uppercase]
wordwrap[line width constraint]
Combining multiple operators
Examples are as follows:
{$title|upper}
{* Take the first 40 characters *}
Topic: {$topic|truncate:40:"..."}
{* Format text string *}
{"now"|date_format:"%Y/%m/%d"}
{* Apply regulator in custom function *}
{mailto|upper address="main@cn-web.com"}
capitalize(capitalize the first letter)
The index.php page is as follows:
$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');
$smarty->display('index.tpl');
The index.tpl page is as follows:
{$articleTitle|capitalize}
OUTPUT output is as follows:
Police Begin Campaign To Rundown Jaywalkers.
count_characters (count the number of characters in the variable)
index.php is as follows:
$smarty->assign('articleTitle', 'Cold Wave Linked to Temperatures.');
$smarty->display('index.tpl');
The index.tpl page is as follows:
{$articleTitle|count_characters}
OUTPUT output is as follows:
Cold Wave Linked to Temperatures.
cat(connection string)
Connect the value in cat to the given variable
index.php is as follows:
$smarty->assign('articleTitle', 'Psychics predict world didn't end');
$smarty->display('index.tpl');
The index.tpl page is as follows:
OUTPUT output is as follows:
count_paragraphs(count number of paragraphs)
Count the number of paragraphs in the variable
index.php is as follows:
$smarty->assign('articleTitle', 'War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.');
$smarty->display('index.tpl');
The index.tpl template page is as follows:
{$articleTitle|count_paragraphs}
OUTPUT output is as follows:
Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.
2
count_sentences(count the number of sentences)
Count the number of sentences in the variable
index.php is as follows:
$smarty->assign('articleTitle', 'Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.');
$smarty->display('index.tpl');
index.tpl template is as follows:
{$articleTitle|count_sentences}
OUTPUT output is as follows:
2
count_words(count words)
Count the number of words in the variable
index.php is as follows:
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
index.tpl template is as follows:
{$articleTitle|count_words}
OUTPUT output is as follows:
7
date_format(date format)
Parameter Position
Parameter location Type Required Default Description
1 string No %b %e, %Y This is the format for the outputted date.
Output string format
2 string No n/a This is the default date if the input is empty.
Default setting when input is empty
Format date and time in the given function serftime();
Timestamps from Unix or mysql (parsable by strtotime) can be passed to smarty.
Designers have full control over date formatting using date_format.
If the data passed to date_format is empty, the second parameter will be used as the time format
index.php is as follows:
$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 output is as follows:
Tuesday, February 6, 2001
14:33:00
Feb 5, 2001
Monday, February 5, 2001
14:33:00
default(default)
Parameter Position Type Required Default Description
1 string No empty This is the default value to output if the variable is empty.
This is the default output when the variable is empty
Set a default value for empty variables.
When the variable is empty or unallocated, the given default value will be output instead.
index.php is as follows:
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
index.tpl template:
{$myTitle|default:"no title"}
OUTPUT output:
no title
escape(transcoding)
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.
Used for html transcoding, url transcoding, converting single quotes on variables without transcoding, hexadecimal transcoding, hexadecimal beautification, or javascript transcoding.
The default is html transcoding
index.php is as follows:
$smarty->assign('articleTitle', "'Stiff Opposition Expected to Casketless Funeral Plan'");
$smarty->display('index.tpl');
index.tpl template:
{$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 (regular replacement)
Find and replace regular expressions .
Parameter Position Type Required Default Description
1 string Yes n/a This is the regular expression to be replaced.
Replace regular expression.
2 string Yes n/a This is the string of text to replace with.
What text string to use to replace
index.php is as follows:
$smarty->assign('articleTitle', "Infertility unlikely tonbe passed on, experts say.");
$smarty->display('index.tpl');
index.tpl template:
{$articleTitle}
{$articleTitle|regex_replace:"/[rtn]/":" "}
OUTPUT output:
be passed on, experts say.
Infertility unlikely to be passed on, experts say.
replace(replace)
Simple search and replace strings
Parameter Position Type Required Default Description
1 string Yes n/a This is the string of text to be replaced.
The string to be replaced
2 string Yes n/a This is the string of text to replace with.
Text to replace
index.php is as follows:
$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");
$smarty->display('index.tpl');
index.tpl template:
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "}
OUTPUT output:
Child's Stool Great for Use in Vineyard.
Child's Stool Great for Use in Garden.
spacify
It is a way to insert spaces or other characters (strings) between each character of a string.
index.php is as follows:
$smarty->assign('articleTitle', 'Something Went Wrong in Jet Crash, Experts Say.');
$smarty->display('index.tpl');
index.tpl template:
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"}
OUTPUT 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 (string format)
Parameter Position Type Required Default Description
1 string Yes n/a This is what format to use. (sprintf)
Formatting method used
Is a method of formatting floating point numbers. For example, decimal numbers. Use sprintf syntax to format
index.php is as follows:
$smarty->assign('number', 23.5787446);
$smarty->display('index.tpl');
index.tpl template:
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}
OUTPUT output:
23.58
24
strip(remove (excess spaces)
Replace all repeated spaces, newlines and tabs with single ones.
index.php is as follows:
$smarty->assign('articleTitle', "Grandmother ofneight makest hole in one.");
$smarty->display('index.tpl');
index.tpl template:
{$articleTitle|strip}
{$articleTitle|strip:" "}
OUTPUT output:
eight makes hole in one.
Grandmother of eight makes hole in one.
Grandmother of eight makes hole in one.
strip_tags (remove html tags)
Remove all tags between , including .
index.php is as follows:
$smarty->assign('articleTitle', "Blind Woman Gets New Kidney from Dad she Hasn't Seen in years." );
$smarty->display('index.tpl');
index.tpl template:
{$articleTitle|strip_tags}
OUTPUT output:
Blind Woman Gets New Kidney from Dad she Hasn't Seen in years.
truncate(interception)
Parameter Position Type Required Default Description
1 integer No 80 This determines how many characters to truncate to.
Specify how many characters to intercept
2 string No ... This is the text to append if truncation occurs.
The string that is intercepted and added after the intercepted word
3 boolean No false This determines whether or not to truncate at a word boundary (false), or at the exact character (true).
Check whether the word boundary is intercepted
Intercept the beginning of the string. The default is 80.
You can specify the second parameter as what character to add after the intercepted string.
By default, smarty will intercept the end of a word,
If you want to intercept exactly how many characters, change the third parameter to "true"
index.php is as follows:
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
$smarty->display('index.tpl');
index.tpl template:
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
OUTPUT 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(uppercase)
Change variables to uppercase
index.php is as follows:
$smarty->assign('articleTitle', "If Strike isn't Settled Quickly it may Last a While.");
$smarty->display('index.tpl');
index.tpl template:
{$articleTitle|upper}
OUTPUT output:
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.
wordwrap (line width constraint)
You can specify the width of the paragraph (that is, how many characters are in a line, if the number of characters exceeds this number, the line will be broken). The default is 80.
The second parameter is optional and can specify what character to use at the constraint point (the default is the newline character n).
By default smarty will intercept to the end of the word, you can also specify how many characters to intercept exactly
Parameter Position Type Required Default Description
1 integer No 80 This determines how many columns to wrap to.
Specify the width of the paragraph (sentence)
2 string No n This is the string used to wrap words with.
What character constraints to use
3 boolean No false This determines whether or not to wrap at a word boundary (false), or at the exact character (true).
Whether to constrain exactly to the character
index.php is as follows:
$smarty->assign('articleTitle', "Blind woman gets new kidney from dad she hasn't seen in years.");
$smarty->display('index.tpl');
index.tpl template:
{$articleTitle|wordwrap:30}
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:"
n"}
{$articleTitle|wordwrap:30:"n":true}
OUTPUT 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.
Combining multiple operators
Operators can be applied to a variable, and they will be applied in combination from left to right. Multiple operators must be separated by the "|" symbol.
The index.php page is as follows:
$smarty->assign('articleTitle', 'Smokers are Productive, but Death Cuts Efficiency.');
$smarty->display('index.tpl');
index.tpl template:
{$articleTitle|upper|spacify}
{$articleTitle|lower|spacify|truncate}
{$articleTitle|lower|truncate:30|spacify}
{$articleTitle|lower|spacify|truncate:30:". . ."}
OUTPUT 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. . .
I hope this article will be helpful to everyone’s PHP programming design.
This is the method.
I tried this:
(put outside the loop)
(increment within the loop)

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment