目錄
filter扩展库简介
定义和用法" >定义和用法
例子" >例子
首頁 後端開發 php教程 php filter函数库 (与变量跟类型有关的扩展),可以过滤常用邮件,IP,变量数组等

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

Jun 13, 2016 pm 01:02 PM
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

?

<?php
print_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
)
登入後複製

?

?filter_list和filter_id结合

?

<?php
echo "<pre class="brush:php;toolbar:false">";
print_r(filter_list());
echo "
登入後複製
"; foreach (filter_list() as $key => $value) { echo "
".$key."=".$value.'='.filter_id($value); } ?> 0=int=257 1=boolean=258 2=float=259 3=validate_regexp=272 4=validate_url=273 5=validate_email=274 6=validate_ip=275 7=string=513 8=stripped=513 9=encoded=514 10=special_chars=515 11=unsafe_raw=516 12=email=517 13=url=518 14=number_int=519 15=number_float=520 16=magic_quotes=521 17=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 
登入後複製

?

?

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.
登入後複製

?

?

?

?

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

?

<?php
$email_a = 'joe@example.com';
$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.";
}
?> 
登入後複製

?以上程序输出:

?

This (email_a) email address is considered valid.
登入後複製

?

验证范例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.";
}
?> 
登入後複製

?以上程序输出:

?

This (ip_a) IP address is considered valid.
登入後複製

?

验证范例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.";
}
?> 
登入後複製

?以上程序会输出:

?

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.
登入後複製

?

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

?

<?php
$a = 'joe@example.org';
$b = 'bogus - at - example dot org';
$c = '(bogus@example.org)';

$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";    
}
?> 
登入後複製

?以上程序会输出:

?

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: (bogus@example.org)
After: bogus@example.org
登入後複製

?

下面介绍一下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
登入後複製
?

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

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

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1320
25
PHP教程
1269
29
C# 教程
1249
24
Python如何使用email、smtplib、poplib、imaplib模組收發郵件 Python如何使用email、smtplib、poplib、imaplib模組收發郵件 May 16, 2023 pm 11:44 PM

一封電子郵件的旅程是:MUA:MailUserAgent-郵件使用者代理程式。 (即類似Outlook的電子郵件軟體)MTA:MailTransferAgent-郵件傳輸代理,就是那些Email服務供應商,例如網易、新浪等等。 MDA:MailDeliveryAgent-郵件投遞代理。 Email服務提供者的某個伺服器寄件者->MUA->MTA->MTA->若

validate函數的作用是什麼 validate函數的作用是什麼 Oct 25, 2023 pm 04:34 PM

validate函數通常用於對輸入資料進行驗證和檢查,以確保其符合特定的規則、格式或條件。其作用是在程式中對輸入資料進行合法性驗證,以提高資料的準確性、完整性和安全性。透過使用validate函數,可以提前檢測和攔截無效或不合法的數據,避免後續程式碼處理錯誤或異常情況。

解決「[Vue warn]: Failed to resolve filter」錯誤的方法 解決「[Vue warn]: Failed to resolve filter」錯誤的方法 Aug 19, 2023 pm 03:33 PM

解決「[Vuewarn]:Failedtoresolvefilter」錯誤的方法在使用Vue進行開發的過程中,我們有時會遇到一個錯誤提示:「[Vuewarn]:Failedtoresolvefilter」。這個錯誤提示通常出現在我們在模板中使用了一個未定義的過濾器的情況下。本文將介紹如何解決這個錯誤並給出相應的程式碼範例。當我們在Vue的

怎麼在SpringBoot2中整合Filter 怎麼在SpringBoot2中整合Filter May 16, 2023 pm 02:46 PM

先定義一個統一存取URL攔截的Filter。程式碼如下:publicclassUrlFilterimplementsFilter{privateLoggerlog=LoggerFactory.getLogger(UrlFilter.class);@OverridepublicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,

Springboot中filter的原理與註冊方法是什麼 Springboot中filter的原理與註冊方法是什麼 May 11, 2023 pm 08:28 PM

1.filter先看下web伺服器的filter所處的位置。 filter是一個前後連接的鏈,前面處理完成之後傳遞給下一個filter處理。 1.1filter的介面定義publicinterfaceFilter{//初始化方法,整個生命週期只執行一次。 //在init方法成功(失敗如拋異常等)執行完前,無法提供過濾服務。 //參數FilterConfig用於取得初始化參數publicvoidinit(FilterConfigfilterConfig)throwsServletException;//

CSS 模糊屬性詳解:filter 與 backdrop-filter CSS 模糊屬性詳解:filter 與 backdrop-filter Oct 20, 2023 pm 04:48 PM

CSS模糊屬性詳解:filter和backdrop-filter導語:在設計網頁時,我們常常需要一些特效來增加頁面的視覺吸引力。而模糊效果是其中一種常見的特效之一。 CSS提供了兩種模糊屬性:filter和backdrop-filter,它們分別用於對元素內容以及背景內容進行模糊處理。本文將詳細介紹這兩個屬性,並提供一些具體的程式碼範例。一、filt

Go中如何使用context實作請求參數校驗 Go中如何使用context實作請求參數校驗 Jul 22, 2023 am 08:23 AM

Go中如何使用context實作請求參數校驗引言:在後端開發過程中,我們經常需要對請求參數進行校驗,以確保參數的合法性。而Go語言提供了context包來處理請求的上下文訊息,其優雅的設計和簡單的使用方式使其成為常用的工具。本文將介紹如何使用Go的context套件來實現請求參數校驗,並給出對應的程式碼範例。 context包簡介在Go中,context包用於傳遞

CSS 視覺屬性解析:box-shadow,text-shadow 和 filter CSS 視覺屬性解析:box-shadow,text-shadow 和 filter Oct 20, 2023 pm 12:51 PM

CSS視覺屬性解析:box-shadow,text-shadow和filter引言:在網頁設計和開發中,使用CSS可以為元素添加各種視覺效果。本文將重點放在CSS中的box-shadow,text-shadow和filter這三個重要屬性,包括其使用方法和效果展示。下面我們分別詳細解析這三個屬性。一、box-shadow(盒子陰影)box-shado

See all articles