WordPress中转义HTML与过滤链接的相关PHP函数使用解析
<p><strong>esc_html()(转义 Html)</strong><br>
esc_html() 函数用来转义 Html 代码,让 Html 代码不转义。</p>
<p>
用法</p>
<div>
<div>
<div id="highlighter_805427">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>esc_html( </code><code>$text</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数</p>
<p>
$text</p>
<p>
(字符串)(必须)要转义的字符串。</p>
<p>
默认值:None</p>
<p>
返回值</p>
<p>
(字符串)返回转义后的字符。</p>
<p>
例子</p>
<div>
<div>
<div id="highlighter_315267">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>echo</code> <code>esc_html( </code><code>'<a href="http://www.example.com/">A link</a>'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
上边的代码将输出:</p>
<p>
WordPress 函数:esc_html()(转义 Html)</p>
<p>
(为了防止浏览器转码,我直接截了一张图)</p>
<p>
更多</p>
<p>
此函数位于:wp-includes/formatting.php</p>
<p>
<br><strong>esc_url()(过滤链接)</strong><br>
很多 URL 会有一些小错误,用 esc_url() 函数可以屏蔽或者修正这些错误,并且可以拒绝不安全的协议。</p>
<p>
esc_url() 函数的工作内容:</p>
<p>
默认拒绝不是下面协议的 URL:defaulting to http、https、ftp、ftps、mailto、news、irc、gopher、nntp、feed 和 telnet<br>
删除无效字符和危险的字符<br>
将字符转换成 HTML 实体字符<br>
使用方法</p>
<div>
<div>
<div id="highlighter_761888">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>esc_url( </code><code>$url</code><code>, </code><code>$protocols</code><code>, </code><code>$_context</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数</p>
<p>
$url</p>
<p>
(字符串)(必须)要被过滤的 URL.</p>
<p>
默认值:None</p>
<p>
$protocols</p>
<p>
(数组)(可选)可以接收协议的数组,如果没有设置,则默认为:defaulting to http、https、ftp、ftps、mailto、news、irc、gopher、nntp、feed 和 telnet.</p>
<p>
默认值:None</p>
<p>
$_context</p>
<p>
(字符串)(可选)如何返回 URL.</p>
<p>
默认值:(字符串)display</p>
<p>
返回值</p>
<p>
(字符串)返回过滤后的链接。</p>
<p>
例子</p>
<div>
<div>
<div id="highlighter_384689">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code><?php </code><code>echo</code> <code>esc_url( </code><code>'www.endskin.com'</code> <code>);</code><code>//输出:http://www.endskin.com ?></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
更多</p>
<p>
此函数位于:wp-includes/formatting.php</p>
頁:
[1]