Home php教程 php手册 php中filter函数验证邮箱、url和ip地址的实例

php中filter函数验证邮箱、url和ip地址的实例

Jun 02, 2016 am 09:13 AM
url verification

在看这函数之前我验证邮箱或IP地址及url都是使用正则表达式来处理,今天发现filter函数可以替换正则哦并且方法简单好用,下面我用实例介绍这函数的用法吧.

早年使用php的时候还不知道有filter这玩意,那时候判断邮箱、url和ip地址格式是否符合都是用正则表达式,后来随着使用的逐渐深入,才知道在php中也可以使用内置的函数库filter来完成这些功能.

1、验证邮箱,先来看原始的正则验证,代码如下:

<?php 
function isEmail($email){ 
    if(preg_match("/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i",$email )) { 
        return &#39;邮箱验证OK&#39;; 
    } else { 
        return &#39;验证不是邮箱&#39;; 
    } 
}
Copy after login

再看filter这玩意,代码如下:

$email = &#39;sjlinyu@qq.com&#39;;  
$result = filter_var($email, FILTER_VALIDATE_EMAIL);  
var_dump($result); //string(14) "sjlinyu@qq.com"
Copy after login

对于filter_var这个函数,如果验证通过则会返回验证对象,否则返回false,感觉后者更简单一些.

2、验证url地址,正则验证,代码如下:

<?php 
$url = &#39;www.phprm.com&#39;; 
$search = &#39;/---正则N---/&#39;; 
if(preg_match($search,$url)){ 
    echo &#39;匹配&#39;; 
}else { 
    echo &#39;不匹配&#39;; 
}
Copy after login

filter_var函数,代码如下:

$url = "http://www.phprm.com";  
$result = filter_var($url, FILTER_VALIDATE_URL);  
var_dump($result); //string(22) "http://www.phprm.com"
Copy after login

3、验证ip地址,正则验证函数,代码如下:

/** 
* 检查IP地址是否正确。 
*/ 
function checkipaddres ($ipaddres) { 
$preg="/A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5])).){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))Z/"; 
if(preg_match($preg,$ipaddres))return true; 
return false; 
}
Copy after login

此函数验证,代码如下:

$url = "192.168.1.110";  
$result = filter_var($url, FILTER_VALIDATE_IP);  
var_dump($result); //string(13) "192.168.1.110"
Copy after login

值的一提的是,这方法也可以用来验证ipv6,代码如下:

$url = "2001:DB8:2de::e13";  
$result = filter_var($url, FILTER_VALIDATE_IP);  
var_dump($result); //string(17) "2001:DB8:2de::e13"
Copy after login

4、验证数值是否为整数,并且在一个整数区间内,代码如下:

$i = &#39;010&#39;;  
$result = filter_var(  
     $i,  
     FILTER_VALIDATE_INT,  
     //设定校验的数值范围  
     array(  
      &#39;options&#39; => array(&#39;min_range&#39; => 1, &#39;max_range&#39; => 100)  
     )  
 );  
var_dump($result);//bool(false)
Copy after login

php的变量是弱类型,如果不用过滤器,直接使用大于小于符号判断的话会是真的,代码如下:

$i = &#39;010&#39;;  
$result = $i >= 1 && $i <= 100;  
var_dump($result);//bool(true)
Copy after login

5、验证浮点数,代码如下:

$float = 12.312;  
$result = filter_var($float, FILTER_VALIDATE_FLOAT);  
var_dump($result); //float(12.312)
Copy after login

在做一些金额方面的验证时,经常需要验证金额是否为浮点数.

总结:php中的filter过滤器虽然比较冷门,但是功能还是蛮强大的,除了上述这些功能外,还有一些过滤输入的功能,可查阅php手册.


本文链接:

收藏随意^^请保留教程地址.

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 regular expressions to verify URL addresses in golang How to use regular expressions to verify URL addresses in golang Jun 24, 2023 pm 04:00 PM

In recent years, with the rapid development of the Internet, our lives are increasingly inseparable from the Internet. When we use the Internet, we often need to enter or provide a URL address. In order to ensure security and accuracy, we need to verify the entered URL address. This article will introduce how to use regular expressions in golang to verify URL addresses. Basic components of a URL address A standard URL address contains the following parts: Protocol: such as http, https, ftp, etc. domain name

How to verify if a URL contains a specified string using regular expressions in PHP How to verify if a URL contains a specified string using regular expressions in PHP Jun 24, 2023 am 09:39 AM

With the development of the Internet, URLs have become an integral part of people's daily lives. Sometimes, we need to validate a URL to determine if it contains a specific string. In PHP, you can use regular expressions to perform this process. This article will introduce how to use regular expressions to verify whether a URL contains a specified string in PHP. First, we need to understand some basic concepts in regular expressions. Regular expressions are expressions used to match string patterns. It is a special syntax used in patterns

How to use regular expressions in golang to verify whether the URL address is a second-level domain name How to use regular expressions in golang to verify whether the URL address is a second-level domain name Jun 25, 2023 pm 06:30 PM

It is a common requirement in golang to use regular expressions to verify whether the URL address is a second-level domain name. In this article, we will introduce how to use regular expressions in golang for verification, and how to write regular expressions to verify whether a URL address is a second-level domain name. First, let's take a look at the basic usage of regular expressions in golang. Golang provides a regular expression library regexp. We can use regular expressions by calling the regexp package. Using regular tables

How to verify if URL address contains query parameters using regular expression in golang How to verify if URL address contains query parameters using regular expression in golang Jun 24, 2023 am 10:52 AM

In golang, using regular expressions to verify whether a URL address contains query parameters can be implemented through go's standard library "regexp". Below we will introduce you to the specific implementation steps. Import the "regexp" package Before using regular expressions, you need to import the "regexp" package first. You can use the following statement to import: import "regexp" defines the regular expression needed to verify whether the URL address contains query parameters.

How to use regular expressions in golang to verify whether the URL address is a fifth-level domain name How to use regular expressions in golang to verify whether the URL address is a fifth-level domain name Jun 24, 2023 am 08:36 AM

Using regular expressions in golang to verify whether a URL address is a fifth-level domain name can be achieved through the following steps: First, we need to understand what a fifth-level domain name is, which refers to a complete domain name consisting of 5 parts, for example: https ://www.example.com.cn, the five parts are: protocol, host name, second-level domain name, third-level domain name and top-level domain name. In this article, we will verify whether a URL address is in the format of a fifth-level domain name. Next, we need to understand

How to use regular expressions in golang to verify whether the input is a complete URL address How to use regular expressions in golang to verify whether the input is a complete URL address Jun 24, 2023 pm 01:30 PM

In golang, it is very important to use regular expressions to verify whether an input is a complete URL address. Regular expressions are a descriptive language that can be used to match text patterns in strings. When we need to verify whether an input conforms to a certain format, regular expressions can help us quickly determine whether the input is correct. So how to use regular expressions in golang to verify whether the input is a complete URL address? This article will explain it to you. First, we need to understand the base of a URL address

How to use regular expressions in golang to verify whether the URL address is a first-level domain name How to use regular expressions in golang to verify whether the URL address is a first-level domain name Jun 24, 2023 am 11:19 AM

In golang, it is very simple to use regular expressions to verify first-level domain names. First, we need to understand what a first-level domain name is. The first-level domain name refers to the highest-level domain name among Internet domain names, usually consisting of one word or abbreviation. For example, com in google.com is the first-level domain name. Therefore, the URL address we want to verify should be in a format similar to www.google.com or abc.baidu.com. Next, we can use golang’s regular

How to validate input of URL parameters using regular expressions in PHP How to validate input of URL parameters using regular expressions in PHP Jun 24, 2023 am 11:10 AM

In web development, the input of URL parameters is very common. It is usually used to pass parameters to the server in order to perform some specific operations or query data. However, since input of URL parameters is not restricted, malicious users can exploit inappropriate input to attack your application. Therefore, validating the input of URL parameters is crucial. In this article, we will cover how to use regular expressions in PHP to validate input of URL parameters. First we need to understand what regular expressions are. Regular expression is a type of word used to match

See all articles