esc_html()

将 & ” ’(小于号,大于号,&,双引号,单引号)编码,转成HTML 实体

描述

将 < > & ” ‘(小于号,大于号,&,双引号,单引号)编码,转成HTML 实体,已经是实体的并不转换。

功能和 esc_attr() 类似。

用法

<?php esc_html( $text ) ?>

参数

$text

(string) (必填) 将编码成实体的文本。

默认值: None

返回值

HTML (string

已经编码成 HTML 实体的文本。

示例

$html = esc_html( ‘<a href=”http://www.example.com/”>A link</a>’ );

$html现在包含以下内容:

&lt;a href=&quot;http://www.example.com/&quot;&gt;A link&lt;/a&gt;

这将在 HTML 文档中显示为:

<a href=“http://www.example.com/”>A link</a>

注意

使用到 the ‘esc_html’ 过滤器.

历史

添加于 版本: 2.8.0

源文件

esc_html() 函数的代码位于 wp-includes/formatting.php.

/**
* Escaping for HTML blocks.
*
* @since 2.8.0
*
* @param string $text
* @return string
*/

function esc_html( $text ) {

$safe_text = wp_check_invalid_utf8( $text );

$safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );

/**

* Filter a string cleaned and escaped for output in HTML.
*
* Text passed to esc_html() is stripped of invalid or special characters
* before output.
*
* @since 2.8.0
*
* @param string $safe_text The text after it has been escaped.
* @param string $text      The text prior to being escaped.
*/

return apply_filters( ‘esc_html’, $safe_text, $text );

}
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索