2010-12-01 124 views
2

我在PHP中匹配的IP地址。所以,我的檢查:PHP preg_match問題與IPv6

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;

有什麼不對?

+2

哇靠這是一個大嚇壞只是正則表達式驗證一個IP地址!你確定它需要那麼複雜? – 2010-12-01 19:48:36

回答

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

您對preg_match的第一個電話沒有使用正則表達式的分隔符。

PHP期望preg_match中的正則表達式被包含在一對分隔符之間。字符/是定界符的流行選擇,它也用於您的第二次致電preg_match。您還可以使用字符如#,|,@,~,!

添加缺少的delimier爲:

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

我會跳過正則表達式和Zend的使用validate模塊。比你發佈的正則表達式更容易,更健壯。

$validator = new Zend_Validate_Ip(); 

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

     // ip appears to be valid 

    } else { 

     // ip is invalid; print the reasons 

    } 
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; 
} 
0

要匹配的IPv4地址,你可以簡單地使用:

preg_match("/\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}/", $string, $result);