preg_match() 函数用于进行正则表达式匹配,成功返回 1 ,否则返回 0 。这篇文章主要介绍了php使用preg_match()函数验证ip地址的方法,涉及php针对数字及字符串的正则匹配操作相关技巧,需要的朋友可以参考下,具体如下:
<?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");
以上就是php使用preg_match()函数验证ip地址的示例代码的详细内容。