转义
escape
用于对变量编码或转义成 html
、url
、单引号
、hex
、hexentity
、javascript
和 mail
。它的默认设置是 html
。
基本用法
参数
参数位置 | 类型 | 必需 | 可能值 | 默认值 | 描述 |
---|---|---|---|---|---|
1 | 字符串 | 否 | html 、htmlall 、url 、urlpathinfo 、quotes 、hex 、hexentity 、javascript 、mail |
html |
这是要使用的转义格式。 |
2 | 字符串 | 否 | ISO-8859-1 、UTF-8 和 htmlentities() 支持的任何字符集 |
UTF-8 |
传递给 htmlentities() 及于其他函数的字符集编码。 |
3 | 布尔值 | 否 | FALSE | TRUE | 双重编码实体,从 & 到 & (仅适用于 html 和 htmlall ) |
示例
<?php
$smarty->assign('articleTitle',
"'Stiff Opposition Expected to Casketless Funeral Plan'"
);
$smarty->assign('EmailAddress','smarty@example.com');
这些示例 escape
行,后面是输出
{$articleTitle}
'Stiff Opposition Expected to Casketless Funeral Plan'
{$articleTitle|escape}
'Stiff Opposition Expected to Casketless Funeral Plan'
{$articleTitle|escape:'html'} {* escapes & " ' < > *}
'Stiff Opposition Expected to Casketless Funeral Plan'
{$articleTitle|escape:'htmlall'} {* escapes ALL html entities *}
'Stiff Opposition Expected to Casketless Funeral Plan'
<a href="?title={$articleTitle|escape:'url'}">click here</a>
<a
href="?title=%27Stiff%20Opposition%20Expected%20to%20Casketless%20Funeral%20Plan%27">click here</a>
{$articleTitle|escape:'quotes'}
\'Stiff Opposition Expected to Casketless Funeral Plan\'
<a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
{$EmailAddress|escape:'mail'} {* this converts to email to text *}
<a href="mailto:%62%6f%..snip..%65%74">bob..snip..et</a>
{'mail@example.com'|escape:'mail'}
smarty [AT] example [DOT] com
{* the "rewind" parameter registers the current location *}
<a href="$my_path?page=foo&rewind={$my_uri|escape:url}">click here</a>
此代码段对于电子邮件很有用,但也参见 {mailto}
{* email address mangled *}
<a href="mailto:{$EmailAddress|escape:'hex'}">{$EmailAddress|escape:'mail'}</a>
另请参见 自动转义、转义 Smarty 解析、{mailto}
以及 混淆电子邮件地址 页面。