English 中文(简体)
PHP preg_match problem with IPv6
原标题:PHP preg_match problem with IPv6

I m 配对PHP中的Pip地址。 因此,我的检查是:

function checkIP($ip){
    $ip = trim($ip);
    if (preg_match("(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]?)", $ip)) return true;
    $v6pattern = "/
(A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6})|
(A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5})|
(A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4})|
(A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3})|
(A([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2})|
(A([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1})|
(A(([0-9a-f]{1,4}:){1,7}|:):)|
(A:(:[0-9a-f]{1,4}){1,7})|
(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}))|
(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}))|
(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})|
(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})|
(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})|
(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})|
(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})|
(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})|
(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})
/x";
    if (preg_match($v6pattern, $ip)) return true;
    return false;
}

然而,I m 发现了一个错误:warning: preg_match (功能.preg-match]:C:xampphtdocsindex. Delimiter绝不能成为甲型数字或反弹。 php on line 5

Line 5 is if (preg_match("(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]?)", $ip)) return true;

什么错误?

最佳回答

你们必须把reg子放在划界者之间。 http://php.net/manual/en/regexp.vis.delimiters.php“rel=“nofollow”>。 也可以使用 过滤器,并避免回收轮椅。 例如:

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
问题回答

Your first call to preg_match does not have a delimiter for the regex used.

PHP期望在preg_match中的reg在限定物的平板之间封闭。 特性/是公众选择划界,在您的第二至次打电话到时也使用这一选择。 您也可使用诸如<代码>#>>、@~!

增加失踪人员:

if (preg_match("#(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]?)#", $ip)) return true;
                ^                                                                                                                                                                  ^

To match an IPv4 address you can simply use:

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





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...