Home  >  Article  >  php教程  >  php验证邮箱和ip地址最简单方法汇总,ip最简

php验证邮箱和ip地址最简单方法汇总,ip最简

WBOY
WBOYOriginal
2016-06-13 08:52:24797browse

php验证邮箱和ip地址最简单方法汇总,ip最简

在开发中验证邮箱、url、数字是我们常用的一些例子,下面整理了验证邮箱、url、数字程序,大家有兴趣可参考一下.

例子代码如下:

public static function isEmail( $email ) 
{ 
return preg_match("/^([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,4}([\.][a-z]{2})?$/i" , $email ); 
} 
public static function isNumber( $num ) 
{ 
return is_numeric( $num ); 
} 
public static function isUrl( $url , $preg = false ) 
{ 
if( $preg ) 
{ 
$status = preg_match ( "/^([^:\/\/])+\:\/\/[\w-]+\.[\w-.\?\/]+$/" , $url ); 
} 
else 
{ 
$status = filter_var( $url , FILTER_VALIDATE_URL ); 
} 
return $status; 
} 

补充:利用php自带函数来操作.

php验证邮箱,代码如下:

$email = 'fengdingbo@gmail.com';             
$result = filter_var($email, FILTER_VALIDATE_EMAIL); 
var_dump($result); // string(20) "fengdingbo@gmail.com" 

php验证url地址,代码如下:

$url = "http://www.bkjia.com"; 
$result = filter_var($url, FILTER_VALIDATE_URL); 
var_dump($result); // string(25) "http://www.bkjia.com" 

php验证ip地址,代码如下:

$url = "192.168.1.110"; 
$result = filter_var($url, FILTER_VALIDATE_IP); 
var_dump($result); // string(13) "192.168.1.110" 
// 该方法也可以用来验证ipv6。 
$url = "2001:DB8:2de::e13";              
$result = filter_var($url, FILTER_VALIDATE_IP); 
var_dump($result); // string(17) "2001:DB8:2de::e13"

以上就是php验证邮箱和ip地址最简单方法,希望对大家的学习有所帮助。

Statement:
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