regex - PHP preg_match problem with IPv6 -
i'm matching ip addresses in php. check is:
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; }
however, i'm getting error: warning: preg_match() [function.preg-match]: delimiter must not alphanumeric or backslash in c:\xampp\htdocs\index.php on line 5
line 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;
what's wrong?
you have place regex between delimiters. check manual. can use filters , avoid reinventing wheel. example:
filter_var($var, filter_validate_ip, filter_flag_ipv6); //for ip's v6
and
filter_var($var, filter_validate_ip, filter_flag_ipv4); //for ip's v4
Comments
Post a Comment