esc_attr__()

将编码转成HTML实体

描述

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

在转义 HTML 属性的时候经常用这个函数(特别是表单的值,比如altvaluetitle等等)

转义并输出值,使用 esc_attr_e() 代替。

用法

 <?php $fname = esc_attr( $text ); ?>

参数

$text

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

默认值: None

返回值

(string) 

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

示例

<?php $fname = ( isset( $_POST[‘fname’] ) ) ? $_POST[‘fname’] : ”; ?>

<input type=“text” name=“fname” value=“<?php echo esc_attr( $fname ); ?>“>

历史

添加于 版本: 2.8.0

源文件

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

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

function esc_attr( $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 an HTML attribute.
*
* Text passed to esc_attr() is stripped of invalid or special characters
* before output.
*
* @since 2.0.6
*
* @param string $safe_text The text after it has been escaped.
* @param string $text      The text prior to being escaped.
*/

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

}

相关

See: Data Validation article for an in-depth discussion of input and output sanitization.

esc_html()

esc_html__()

esc_html_e()

esc_attr()

esc_attr__()

esc_attr_e()

esc_js()

esc_sql()

esc_textarea()

esc_url()

esc_url_raw()

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索