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

WBOY
Release: 2016-05-16 20:05:35
Original
1823 people have browsed it

在开发中验证邮箱、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; }
Copy after login

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

php验证邮箱,代码如下:

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

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

$url = "http://www.jb51.net"; $result = filter_var($url, FILTER_VALIDATE_URL); var_dump($result); // string(25) "http://www.jb51.net"
Copy after login

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"
Copy after login

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

source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!