使用正则表达式验证 IPv4 和 IPv6 的 PHP 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9608001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:15:07  来源:igfitidea点击:

PHP function to validate IPv4 and IPv6 using regex

phpregexstringvalidation

提问by chris

I'm needing to create a function for IPv4 and v6 that I cause use local ip's as well.

我需要为 IPv4 和 v6 创建一个函数,我导致它也使用本地 ip。

What I know is a valid IPv4 ranges from 0.0.0.0 to 255.255.255.255 What I know of IPv6 is limited however as despite it being around for a while I haven't really looked much into it til today. But I want to future proof the function I am making a little bit while keeping it a bit retro for the time being. I'm not sure what the valid ranges are for IPv6.

我所知道的是有效的 IPv4 范围从 0.0.0.0 到 255.255.255.255 我对 IPv6 的了解是有限的,尽管它已经存在了一段时间,但直到今天我还没有真正研究它。但我想在未来证明我正在制作的功能,同时暂时保持它有点复古。我不确定 IPv6 的有效范围是什么。

Anyway In general what I am thinking is a function to the extent of

无论如何总的来说,我在想的是一个函数

function validateIP($ip, $vSix = NULL)
{
    if($vSix !== NULL)
    {
      if(preg_match([regex-to-validate-ipv6], $ip))
      {
        return true;
      }
      else
      {
        return false;
      }
    }

    if(preg_match([regex-to-validate-ipv4], $ip))
    {
      return true;
    }
    else
    {
      return false;
    }
}

my thing is I suck with regex so I have no idea how to write one that will validate v4 or 6. Also a sanity check on the above function concept would be nice as well.

我的事情是我对正则表达式很烂,所以我不知道如何编写一个可以验证 v4 或 6 的正则表达式。另外,对上述函数概念进行完整性检查也很好。

回答by Morgon

Check PHP's filter_varfunction. It has a number of validators, including IPv4 and IPv6.

检查PHP的filter_var功能。它有许多验证器,包括 IPv4 和 IPv6。

$isValid = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
// $isValid can be evaluated as boolean, as it's FALSE if validation fails.

回答by Paul

Edit

编辑

Please see Natxet's comment on this answer, and Morgon's answer for a better solution.

请参阅 Natxet 对此答案的评论,以及 Morgon 的答案以获得更好的解决方案。

Original Answer

原答案

You can just use inet_pton. It returns false if the IP is not a valid IPv6 or IPv4:

你可以只使用inet_pton。如果 IP 不是有效的 IPv6 或 IPv4,则返回 false:

function validateIP($ip){
    return inet_pton($ip) !== false;
}

回答by Mark Pro Campos

try

尝试

//validates IPV4 and IPV6
$isValid = filter_var($ip, FILTER_VALIDATE_IP));

//validates IPV4
$isValid = filter_var($ip, FILTER_VALIDATE_IP,FILTER_FLAG_IPV4));

//validates IPV6
$isValid = filter_var($ip, FILTER_VALIDATE_IP,FILTER_FLAG_IPV6));

http://www.php.net/manual/en/filter.filters.validate.php

http://www.php.net/manual/en/filter.filters.validate.php

回答by RafaSashi

In addition to Morgon and Mark Pro Campos answers:

除了 Morgon 和 Mark Pro Campos 的回答:

function is_valid_ip($ip='', $ip_type=''){

    $isValid=false;

    if($ip_type=='ipv4'){

        //validates IPV4
        $isValid = filter_var($ip, FILTER_VALIDATE_IP,FILTER_FLAG_IPV4);
    }
    elseif($ip_type=='ipv6'){

        //validates IPV6
        $isValid = filter_var($ip, FILTER_VALIDATE_IP,FILTER_FLAG_IPV6);
    }
    else{

        //validates IPV4 and IPV6
        $isValid = filter_var($ip, FILTER_VALIDATE_IP);
    }

    if($isValid == $ip){

        $isValid=true;
    }

    return $isValid;
}

Resources

资源

http://php.net/manual/en/function.filter-var.php

http://php.net/manual/en/function.filter-var.php

回答by Aeron

Try:

尝试:

IPv4

IPv4

/^((2[0-4]|1\d|[1-9])?\d|25[0-5])(\.(?1)){3}\z/

IPv6

IPv6

/^(((?=(?>.*?(::))(?!.+)))?|([\dA-F]{1,4}(|:(?!$)|$)|))(?4){5}((?4){2}|((2[0-4]|1\d|[1-9])?\d|25[0-5])(\.(?7)){3})\z/i

From: http://home.deds.nl/~aeron/regex/

来自:http: //home.deds.nl/~aeron/regex/

回答by enygma

You might want to check into filter_var instead. It has a filter for IP addresses (IPv4 and IPv6): http://us.php.net/manual/en/filter.filters.validate.php

您可能想改为检查 filter_var。它有一个 IP 地址过滤器(IPv4 和 IPv6):http: //us.php.net/manual/en/filter.filters.validate.php