我正在使用我在浏览时遇到的IP验证功能,它一直运行良好,直到今天我遇到了问题.
由于某种原因,该功能不会将此IP验证为有效:203.81.192.26
我对正则表达式并不太满意,所以会对可能出错的任何帮助表示感谢.
如果你有其他功能,如果你能为我发布,我将不胜感激.
该功能的代码如下:
public static function validateIpAddress($ip_addr)
{
global $errors;
$preg = '#^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}' .
'(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$#';
if(preg_match($preg, $ip_addr))
{
//now all the intger values are separated
$parts = explode(".", $ip_addr);
//now we need to check each part can range from 0-255
foreach($parts as $ip_parts)
{
if(intval($ip_parts) > 255 || intval($ip_parts) < 0)
{
$errors[] = "ip address is not valid.";
return false;
}
return true;
}
return true;
} else {
$errors[] = "please double check the ip address.";
return false;
}
}