Smarty:escape

2018-10-12 15:31 更新

escape

escape可用于將變量編碼或轉換成 htmlurl單引號十六進制十六進制實體javascript和 電郵地址。 默認是:html

參數(shù)順序類型必選參數(shù)允許取值默認值說明
1stringNohtmlhtmlallurl,urlpathinfo單引號十六進制,十六進制實體javascript電郵地址html這是escape轉換后的格式
2stringNoISO-8859-1UTF-8, 和其他htmlentities()支持的字符集UTF-8傳遞給htmlentities()的字符集類型
3booleanNoFALSETRUE兩次轉換實體,& 到 & (僅在html 和 htmlall 使用)

Example 5.10. escape

<?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}
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;

{$articleTitle|escape:'html'}    {* escapes  & " ' < > *}
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;

{$articleTitle|escape:'htmlall'} {* escapes ALL html entities *}
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;

<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">&#x62;&#x6f;&#x62..snip..&#x65;&#x74;</a>

{'mail@example.com'|escape:'mail'}
smarty [AT] example [DOT] com

Example 5.11. escape的另一個例子

{*  "rewind"參數(shù)是當前URL地址 *}
<a href="$my_path?page=foo&rewind=$my_uri|urlencode}">click here</a>

這個方法很適合用在電郵地址上,另外可以參見 {mailto}

{* 電郵地址編碼 *}
<a href="mailto:{$EmailAddress|escape:'hex'}">{$EmailAddress|escape:'mail'}</a>

參見 Smarty編譯轉換, {mailto} 和 混淆電郵地址。

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號