Heim  >  Artikel  >  Backend-Entwicklung  >  详解WordPress开发中过滤属性以及Sql语句的函数使用_php实例

详解WordPress开发中过滤属性以及Sql语句的函数使用_php实例

WBOY
WBOYOriginal
2016-05-16 20:01:551058Durchsuche

esc_attr()(过滤属性)
一般在写 Html 代码的标签属性的时候会是下边的格式:

<input type="text" name="rep" value="rep_value" />

那如果 value 属性是动态输出的呢?

<input type="text" name="rep" value="<&#63;php echo get_option( 'rep_value' ); &#63;>" />

但是,如果动态输出的属性里有双引号、尖括号等特殊字符,Html 代码就会被打乱,这时就可以使用 esc_attr() 函数对输出的属性进行转义。

使用方法

esc_attr( $text );

参数

$text (字符串)(必须)要转义的字符串。 默认值:None

返回值

返回转义后的字符串。

例子

<input type="text" name="rep" value="<&#63;php echo esc_attr( get_option( 'rep_value' ) ); &#63;>" />

其它

此函数位于:wp-includes/formatting.php

esc_sql()(过滤 Sql 语句)
esc_sql() 用来过滤准备添加到 Sql 语句里边的字符串,防止 Sql 注入和 Sql 语句被数据干扰出现异常。

用法

esc_sql( $data );

参数

$data

(字符串)(必须)要过滤的字符串。

默认值:None

返回值

(字符串)返回过滤后的字符串,可以直接添加到 Sql 语句里。

例子

$name = esc_sql( $name );
$status = esc_sql( $status );
$wpdb->get_var( "SELECT something FROM table WHERE foo = '$name' and status = '$status'" );

更多

此函数位于:wp-includes/formatting.php


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn