Welcome

首页 / 网页编程 / PHP / php使用preg_match()函数验证ip地址的方法

本文实例讲述了php使用preg_match()函数验证ip地址的方法。分享给大家供大家参考,具体如下:
<?php/**@return Boolen*@param String $ip 要匹配的ip地址*@param String $pat 匹配的正则规则*@param Boolen 匹配成功后返回的布尔值*preg_match()*0为不成功,1为成功*/function fun($ip){//0.0.0.0--- 255.255.255.255$pat = "/^(((1?d{1,2})|(2[0-4]d)|(25[0-5])).){3}((1?d{1,2})|(2[0-4]d)|(25[0-5]))$/";if(preg_match($pat,$ip)){$num = preg_match($pat,$ip);return $num;}else{$num = preg_match($pat,$ip);return $num;}}echo fun("255.255.255.255");
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php正则表达式用法总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。