Home >php教程 >PHP源码 >php 验证邮箱、url、数字程序代码

php 验证邮箱、url、数字程序代码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-08 17:21:061102browse

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

<script>ec(2);</script>

例子

 代码如下 复制代码

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.111cn.net";
$result = filter_var($url, FILTER_VALIDATE_URL);
var_dump($result); // string(25) "http://www.111cn.net"

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"

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