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

WBOY
Release: 2016-05-16 20:01:55
Original
1068 people have browsed it

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

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

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

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

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

使用方法

esc_attr( $text );
Copy after login

参数

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

返回值

返回转义后的字符串。

例子

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

其它

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

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

用法

esc_sql( $data );
Copy after login

参数

$data

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

默认值:None

返回值

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

例子

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

Copy after login

更多

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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!