Table of Contents
filter扩展库简介
定义和用法" >定义和用法
语法" >语法
例子" >例子
Home Backend Development PHP Tutorial php filter函数库 (与变量跟类型有关的扩展),可以过滤常用邮件,IP,变量数组等

php filter函数库 (与变量跟类型有关的扩展),可以过滤常用邮件,IP,变量数组等

Jun 13, 2016 am 11:03 AM
email filter validate

php filter函数库 (与变量和类型有关的扩展),可以过滤常用邮件,IP,变量数组等

?

filter扩展库简介

?

This extension filters data by either validating or sanitizing it. This is especially useful when the data source contains unknown (or foreign) data, like user supplied input. For example, this data may come from an HTML form.

There are two main types of filtering: validation and sanitization.

Validation is used to validate or check if the data meets certain qualifications. For example, passing in FILTER_VALIDATE_EMAIL will determine if the data is a valid email address, but will not change the data itself.

Sanitization will sanitize the data, so it may alter it by removing undesired characters. For example, passing in FILTER_SANITIZE_EMAIL will remove characters that are inappropriate for an email address to contain. That said, it does not validate the data.

Flags are optionally used with both validation and sanitization to tweak behaviour according to need. For example, passing in FILTER_FLAG_PATH_REQUIRED while filtering an URL will require a path (like /foo in http://example.org/foo) to be present.

?

这个扩展过滤器数据通过验证和消毒。这是特别有用,当数据源包含未知(或外国)数据,比如用户提供的输入。例如,这个数据可能来自一个HTML表单。  有两种主要类型的过滤:验证和数据消毒。  验证是用于验证或检查数据满足一定的条件。例如,通过在滤波器验证电子邮件将决定如果数据是一个有效的电子邮件地址,但不会改变数据本身。  将清洁卫生处理的数据,所以它可能改变它通过消除不受欢迎的人物。例如,通过在过滤净化电子邮件将删除字符不含有一个电子邮件地址。据说,它不验证数据。  旗帜是选择性地用于验证和数据消毒根据需要来调整行为。例如,passin

?

过滤的函数列表:

函数名 功能

filter_list 过滤器列表——返回一个列表的所有支持的过滤器

filter_id 过滤器ID -返回过滤器ID属于一个名叫过滤器

filter_has_var 过滤器已经var -检查如果变量指定类型的存在

filter_input 过滤输入——得到一个特定的外部变量的名称,并选择性地过滤它

filter_input_array 过滤输入数组——得到外部变量和选择性地过滤它们

filter_var 使用filter_var过滤变量指定的过滤器

filter_var_array 过滤器var数组——得到多个变量和选择性地过滤它们

?

?

?

?

函数示例:

filter_list

?

<?phpprint_r(filter_list());?> 以上例程的输出类似于:Array(    [0] => int    [1] => boolean    [2] => float    [3] => validate_regexp    [4] => validate_url    [5] => validate_email    [6] => validate_ip    [7] => string    [8] => stripped    [9] => encoded    [10] => special_chars    [11] => unsafe_raw    [12] => email    [13] => url    [14] => number_int    [15] => number_float    [16] => magic_quotes    [17] => callback)
Copy after login

?

?filter_list和filter_id结合

?

<?phpecho "<pre class="brush:php;toolbar:false">";print_r(filter_list());echo "
Copy after login
";foreach (filter_list() as $key => $value){echo "
".$key."=".$value.'='.filter_id($value);}?>0=int=2571=boolean=2582=float=2593=validate_regexp=2724=validate_url=2735=validate_email=2746=validate_ip=2757=string=5138=stripped=5139=encoded=51410=special_chars=51511=unsafe_raw=51612=email=51713=url=51814=number_int=51915=number_float=52016=magic_quotes=52117=callback=1024

?

?filter_id

?

<?php $filters = filter_list(); foreach($filters as $filter_name) {     echo $filter_name .": ".filter_id($filter_name) ."<br>"; } ?> Will result in: boolean: 258 float: 259 validate_regexp: 272 validate_url: 273 validate_email: 274 validate_ip: 275 string: 513 stripped: 513 encoded: 514 special_chars: 515 unsafe_raw: 516 email: 517 url: 518 number_int: 519 number_float: 520 magic_quotes: 521 callback: 1024 
Copy after login

?

?

filter_has_var 函数效率:

To note: filter_has_var() is a bit faster than isset()

翻译:filter_has_var函数比isset()快一点

?

Please note that the function does not check the live array, it actually checks the content received by php:

翻译:请注意,这个函数不检查包括数组,它只检查PHP接收到的内容

?

?

<?php$_GET['test'] = 1;echo filter_has_var(INPUT_GET, 'test') ? 'Yes' : 'No';?>would say "No", unless the parameter was actually in the querystring.Also, if the input var is empty, it will say Yes.
Copy after login

?

?

?

?

验证范例1(验证邮箱):

?

<?php$email_a = [email&#160;protected]';$email_b = 'bogus';if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {    echo "This (email_a) email address is considered valid.";}if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {    echo "This (email_b) email address is considered valid.";}?> 
Copy after login

?以上程序输出:

?

This (email_a) email address is considered valid.
Copy after login

?

验证范例2(验证IP)

?

<?php$ip_a = '127.0.0.1';$ip_b = '42.42';if (filter_var($ip_a, FILTER_VALIDATE_IP)) {    echo "This (ip_a) IP address is considered valid.";}if (filter_var($ip_b, FILTER_VALIDATE_IP)) {    echo "This (ip_b) IP address is considered valid.";}?> 
Copy after login

?以上程序输出:

?

This (ip_a) IP address is considered valid.
Copy after login

?

验证范例3(通过选项来过滤变量):

?

<?php$int_a = '1';$int_b = '-1';$int_c = '4';$options = array(    'options' => array(                      'min_range' => 0,                      'max_range' => 3,                      ));if (filter_var($int_a, FILTER_VALIDATE_INT, $options) !== FALSE) {    echo "This (int_a) integer is considered valid (between 0 and 3).\n";}if (filter_var($int_b, FILTER_VALIDATE_INT, $options) !== FALSE) {    echo "This (int_b) integer is considered valid (between 0 and 3).\n";}if (filter_var($int_c, FILTER_VALIDATE_INT, $options) !== FALSE) {    echo "This (int_c) integer is considered valid (between 0 and 3).\n";}$options['options']['default'] = 1;if (($int_c = filter_var($int_c, FILTER_VALIDATE_INT, $options)) !== FALSE) {    echo "This (int_c) integer is considered valid (between 0 and 3) and is $int_c.";}?> 
Copy after login

?以上程序会输出:

?

This (int_a) integer is considered valid (between 0 and 3).This (int_c) integer is considered valid (between 0 and 3) and is 1.
Copy after login

?

消失有害字符并且验证示例1:

?

<?php$a = [email&#160;protected]';$b = 'bogus - at - example dot org';$c = '([email&#160;protected])';$sanitized_a = filter_var($a, FILTER_SANITIZE_EMAIL);if (filter_var($sanitized_a, FILTER_VALIDATE_EMAIL)) {    echo "This (a) sanitized email address is considered valid.\n";}$sanitized_b = filter_var($b, FILTER_SANITIZE_EMAIL);if (filter_var($sanitized_b, FILTER_VALIDATE_EMAIL)) {    echo "This sanitized email address is considered valid.";} else {    echo "This (b) sanitized email address is considered invalid.\n";}$sanitized_c = filter_var($c, FILTER_SANITIZE_EMAIL);if (filter_var($sanitized_c, FILTER_VALIDATE_EMAIL)) {    echo "This (c) sanitized email address is considered valid.\n";    echo "Before: $c\n";    echo "After:  $sanitized_c\n";    }?> 
Copy after login

?以上程序会输出:

?

This (a) sanitized email address is considered valid.This (b) sanitized email address is considered invalid.This (c) sanitized email address is considered valid.Before: ([email&#160;protected])After: [email&#160;protected]
Copy after login

?

下面介绍一下filter_input,摘自百度百科:

?

定义和用法

filter_input() 函数从脚本外部获取输入,并进行过滤。

  本函数用于对来自非安全来源的变量进行验证,比如用户的输入。

  本函数可从各种来源获取输入:

  INPUT_GET

  INPUT_POST

  INPUT_COOKIE

  INPUT_ENV

  INPUT_SERVER

  INPUT_SESSION (Not yet implemented)

  INPUT_REQUEST (Not yet implemented)

  如果成功,则返回被过滤的数据,如果失败,则返回 false,如果?variable?参数未设置,则返回 NULL。

语法

  filter_input(input_type, variable, filter, options)

参数描述
input_type必需。规定输入类型。参见上面的列表中可能的类型。
variable规定要过滤的变量。
filter可选。规定要使用的过滤器的 ID。默认是 FILTER_SANITIZE_STRING。?
请参见完整的 PHP Filter 函数参考手册,获得可能的过滤器。?
过滤器 ID 可以是 ID 名称 (比如 FILTER_VALIDATE_EMAIL),或 ID 号(比如 274)。
options规定包含标志/选项的数组。检查每个过滤器可能的标志和选项。

例子

  在本例中,我们使用 filter_input() 函数来过滤一个 POST 变量。所接受的 POST 变量是合法的 e-mail 地址。

  

?

  { echo "E-Mail is not valid"; }  else  { echo "E-Mail is valid"; }  ?>  输出类似:  E-Mail is valid
Copy after login
?

过滤和验证字串的过滤类型详细请见PHP官方手册

写这篇内容的时候发现以前有朋友写过了,给个链接可以查看一下更多?http://blog.johnsonlu.org/phpfilter/

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use email, smtplib, poplib, imaplib modules to send and receive emails in Python How to use email, smtplib, poplib, imaplib modules to send and receive emails in Python May 16, 2023 pm 11:44 PM

The journey of an email is: MUA: MailUserAgent - Mail User Agent. (i.e. email software similar to Outlook) MTA: MailTransferAgent - Mail transfer agent, which is those email service providers, such as NetEase, Sina, etc. MDA: MailDeliveryAgent - Mail delivery agent. A server of the Email service provider sender->MUA->MTA->MTA->if

What is the function of validate function What is the function of validate function Oct 25, 2023 pm 04:34 PM

The validate function is typically used to validate and check input data to ensure that it conforms to specific rules, formats, or conditions. Its function is to verify the legality of input data in the program to improve the accuracy, integrity and security of the data. By using the validate function, invalid or illegal data can be detected and intercepted in advance to avoid subsequent code processing errors or exceptions.

How to solve the '[Vue warn]: Failed to resolve filter' error How to solve the '[Vue warn]: Failed to resolve filter' error Aug 19, 2023 pm 03:33 PM

Methods to solve the "[Vuewarn]:Failedtoresolvefilter" error During the development process using Vue, we sometimes encounter an error message: "[Vuewarn]:Failedtoresolvefilter". This error message usually occurs when we use an undefined filter in the template. This article explains how to resolve this error and gives corresponding code examples. When we are in Vue

What is the principle and registration method of filter in Springboot What is the principle and registration method of filter in Springboot May 11, 2023 pm 08:28 PM

1. Filter First look at the location of the filter of the web server. Filter is a chain connected before and after. After the previous processing is completed, it is passed to the next filter for processing. 1.1Filter interface definition publicinterfaceFilter{//Initialization method, only executed once in the entire life cycle. //Filtering services cannot be provided until the init method is successfully executed (failure such as throwing an exception, etc.). //The parameter FilterConfig is used to obtain the initialization parameter publicvoidinit(FilterConfigfilterConfig)throwsServletException;//

How to filter in java How to filter in java Apr 18, 2023 pm 11:04 PM

Note 1. If the Lambda parameter generates a true value, the filter (Lambda that can generate a boolean result) will generate an element; 2. When false is generated, this element will no longer be used. Example to create a List collection: ListstringCollection=newArrayList();stringCollection.add("ddd2");stringCollection.add("aaa2");stringCollection.add("bbb1");stringC

Detailed explanation of CSS blur properties: filter and backdrop-filter Detailed explanation of CSS blur properties: filter and backdrop-filter Oct 20, 2023 pm 04:48 PM

Detailed explanation of CSS fuzzy attributes: filter and background-filter Introduction: When designing web pages, we often need some special effects to increase the visual appeal of the page. The blur effect is one of the common special effects. CSS provides two blur attributes: filter and background-filter, which are used to blur element content and background content respectively. This article explains these two properties in detail and provides some concrete code examples. 1. filter

How to use context to implement request parameter verification in Go How to use context to implement request parameter verification in Go Jul 22, 2023 am 08:23 AM

How to use context to implement request parameter verification in Go Introduction: During the back-end development process, we often need to verify the request parameters to ensure the legality of the parameters. The Go language provides the context package to handle request context information. Its elegant design and simple use make it a commonly used tool. This article will introduce how to use Go's context package to implement request parameter verification and give corresponding code examples. Introduction to the context package In Go, the context package is used to deliver

Tutorial on how to insert attachments into win10 mailbox Tutorial on how to insert attachments into win10 mailbox Jan 07, 2024 pm 12:14 PM

Many users need to send emails for work in their daily lives, and some even need to attach various plug-in materials for communication. So how to insert attachments? Let’s take a look at the detailed tutorial below. How to insert attachments to win10 mailbox: 1. Open the mailbox 2. Click the "New Mail" icon in the upper left corner 3. Click "Insert" in the upper right corner 4. Click "Attachment" in the upper right corner 5. Select the required "Attachment" 6. Complete

See all articles