教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php中filter函数验证邮箱、url和ip地址的实例

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

发布时间:2016-12-03   编辑:jiaochengji.com
教程集为您提供php中filter函数验证邮箱、url和ip地址的实例等资源,欢迎您收藏本站,我们将为您提供最新的php中filter函数验证邮箱、url和ip地址的实例资源
在看这函数之前我验证邮箱或IP地址及url都是使用正则表达式来处理,今天发现filter函数可以替换正则哦并且方法简单好用,下面我用实例介绍这函数的用法吧。

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

1、验证邮箱

先来看原始的正则验证

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4941')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4941>

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

再看filter这玩意

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1595')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1595>

$email = 'sjlinyu@qq.com'; 

$result = filter_var($email, FILTER_VALIDATE_EMAIL); 

var_dump($result); //string(14) "sjlinyu@qq.com"

对于filter_var这个函数,如果验证通过则会返回验证对象,否则返回false。

感觉后者更简单一些哦

2、验证url地址

正则验证

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3166')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3166>

<?php
$url = 'www.jiaochengji.com';
$search = '/---正则N---/';
if(preg_match($search,$url)){
 echo '匹配';
}else {
 echo '不匹配';
}
?>

filter_var函数

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9656')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9656>

$url = "http://www.jiaochengji.com"; 

$result = filter_var($url, FILTER_VALIDATE_URL); 

var_dump($result); //string(22) "http://www.jiaochengji.com"

3、验证ip地址

正则验证函数

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5636')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5636>

/**
* 检查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;
}


 

此函数验证

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8219')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8219>

$url = "192.168.1.110"; 

$result = filter_var($url, FILTER_VALIDATE_IP); 

var_dump($result); //string(13) "192.168.1.110"

值的一提的是,这方法也可以用来验证ipv6。

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1536')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1536>

$url = "2001:DB8:2de::e13"; 

$result = filter_var($url, FILTER_VALIDATE_IP); 

var_dump($result); //string(17) "2001:DB8:2de::e13"

验证数值是否为整数,并且在一个整数区间内

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2549')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2549>


$i = '010'; 

$result = filter_var( 

     $i, 

     FILTER_VALIDATE_INT, 

     //设定校验的数值范围 

     array( 

      'options' => array('min_range' => 1, 'max_range' => 100) 

     ) 

 ); 

var_dump($result);//bool(false)

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7730')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7730>


 $i = '010'; 

$result = $i >= 1 && $i <= 100; 

 var_dump($result);//bool(true)

5、验证浮点数

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4714')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4714>


$float = 12.312; 

$result = filter_var($float, FILTER_VALIDATE_FLOAT); 

var_dump($result); //float(12.312)

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

总结

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

您可能感兴趣的文章:
php中filter函数用法之验证邮箱、url和ip地址的方法
php过滤器filter验证邮箱、url和ip地址等
php中filter函数验证邮箱、url和ip地址的实例
php 获取网站地址的函数代码
php邮箱检测的正则表达式一例
PHP邮箱地址正确性验证示例
php正则验证邮箱的函数
jquery无刷新验证邮箱地址实现实例
PHP如何使用filter_var()函数?(代码示例)
php中通过curl smtp发送邮件的例子

[关闭]
~ ~