Home > php教程 > php手册 > php中filter函数验证、过滤用户输入的数据

php中filter函数验证、过滤用户输入的数据

WBOY
Release: 2016-06-13 09:45:17
Original
1045 people have browsed it

PHP Filter 简介

PHP 过滤器用于对来自非安全来源的数据(比如用户输入)进行验证和过滤。

复制代码 代码如下:


//除去html标签,或除去编码特殊字符
var_dump(filter_var("中文ABC@#<script>abc</script>BBB",FILTER_SANITIZE_STRING));

//url_encoded编码,除去或编码特殊字符
var_dump(filter_var("http://中文啊",FILTER_SANITIZE_ENCODED));

//html转义字符""&以及ASCII值小于32的字符。
var_dump(filter_var("",FILTER_SANITIZE_SPECIAL_CHARS));

//删除所有字符,除了字母、数字以及 !#$%&'*+-/=?^_`{|}~@.[]
var_dump(filter_var("AS$&>
//删除所有字符,除了字母、数字以及 $-_.+!*'(),{}|\\^~[]`#%";/?:@&=
var_dump(filter_var("k!",FILTER_SANITIZE_URL));

//删除所有字符,除了数字和+-外
var_dump(filter_var("123ABC++",FILTER_SANITIZE_NUMBER_INT));

//删除所有字符,除了数字、+- 以及 .,eE。
var_dump(filter_var("mm.,pp",FILTER_SANITIZE_NUMBER_FLOAT));

//应用addslashes
var_dump(filter_var("aa//''",FILTER_SANITIZE_MAGIC_QUOTES));

//在指定的范围以整数验证值
var_dump(filter_var(20,FILTER_VALIDATE_INT,array("options"=>array("min_range"=>10,"max_range"=>51))));

//如果是 "1", "true", "on" 以及 "yes",则返回 true,如果是 "0", "false", "off", "no" 以及 "",则返回 false。否则返回 NULL。
var_dump(filter_var(1,FILTER_VALIDATE_BOOLEAN));

//以浮点数验证值。
var_dump(filter_var(222,FILTER_VALIDATE_FLOAT));

//验证URL
var_dump(filter_var("http://www.baidu.com",FILTER_VALIDATE_URL));

//验证email
var_dump(filter_var("abcd@123.com",FILTER_VALIDATE_EMAIL));

//验证IP地址
var_dump(filter_var("1.1.1.266",FILTER_VALIDATE_IP));

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template