PHP的preg_match问题与IPv6有关

时间:2010-12-01 19:46:08

标签: php regex ipv6

我在PHP中匹配ip地址。所以我的检查是:

function checkIP($ip){
    $ip = trim($ip);
    if (preg_match("\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", $ip)) return true;
    $v6pattern = "/
(\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6}\Z)|
(\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}\Z)|
(\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}\Z)|
(\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}\Z)|
(\A([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}\Z)|
(\A([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1}\Z)|
(\A(([0-9a-f]{1,4}:){1,7}|:):\Z)|
(\A:(:[0-9a-f]{1,4}){1,7}\Z)|
(\A((([0-9a-f]{1,4}:){6})(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})\Z)|
(\A(([0-9a-f]{1,4}:){5}[0-9a-f]{1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})\Z)|
(\A([0-9a-f]{1,4}:){5}:[0-9a-f]{1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|
(\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|
(\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,3}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|
(\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,2}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|
(\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,1}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|
(\A(([0-9a-f]{1,4}:){1,5}|:):(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|
(\A:(:[0-9a-f]{1,4}){1,5}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)
/x";
    if (preg_match($v6pattern, $ip)) return true;
    return false;
}

但是,我收到了错误:warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\index.php on line 5

第5行是if (preg_match("\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", $ip)) return true;

怎么了?

5 个答案:

答案 0 :(得分:5)

我会跳过正则表达式并使用zend的validate模块。比你发布的正则表达式更容易和更强大。

  $validator = new Zend_Validate_Ip();

  if ($validator->isValid($ip)) {

      // ip appears to be valid

  } else {

      // ip is invalid; print the reasons

  }

答案 1 :(得分:5)

您必须将正则表达式放在分隔符之间。检查Manual。您也可以使用filters并避免重新发明轮子。例如:

filter_var($var, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); //for IP's V6

filter_var($var, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); //for IP's V4

答案 2 :(得分:2)

您对preg_match的第一次调用没有使用正则表达式的分隔符。

PHP希望preg_match中的正则表达式包含在一对分隔符之间。角色/是受欢迎的分隔符选择,也可用于您对preg_match的第二次调用。您还可以使用#|@~!等字符。

将缺少的分隔符添加为:

if (preg_match("#\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b#", $ip)) return true;
                ^                                                                                                                                                                  ^

答案 3 :(得分:0)

如果您不介意IPv4和IPv6地址,那么通过inet_ntopinet_pton进行投放会更方便,例如。

function is_ip($address) {
  $packed = inet_pton ($address);
  if (FALSE == $packed || FALSE === inet_ntop ($packed)) {
    return FALSE;
  }
  return TRUE;
}

答案 4 :(得分:0)

要匹配IPv4地址,您只需使用:

  

preg_match(“/ \ d {1,3}。\ d {1,3}。\ d {1,3}。\ d {1,3} /”,$ string,$ result);