验证 IP 地址的特殊形式

发布于 2024-12-01 03:22:35 字数 447 浏览 2 评论 0原文

我遇到了一个奇怪的 IP,它的八位字节中有多余的零值。是否有办法正确验证其作为 IP 或使用正则表达式删除那些多余的零?

示例如下: 218.064.215.239(注意第二个八位位组“064”处的额外零)。

我确实有一个有效的 IP 验证函数,但由于正则表达式的性质无法接受额外的零,因此它无法正确验证该 Ip。以下是 PHP 代码:

function valid_ip($ip) {
    return preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" .
            "(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $ip);
} 

感谢您提前提供的任何帮助! :D

I have encountered a strange IP which has redundant zero values among the octets. Is there anyway to properly validate this as an IP or use regular expression to remove those redundant zeroes?

example is of follows:
218.064.215.239 (take note of the extra zero at the second octet "064").

I do have one working IP validation function but it will not validiate this Ip properly due to the nature of the regular expression unable to accept that extra zero. Following is the code in PHP:

function valid_ip($ip) {
    return preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" .
            "(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $ip);
} 

thanks for any help in advance peeps! :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

红颜悴 2024-12-08 03:22:35

这将纠正它们:

$ip = "123.456.007.89";
$octets = explode(".", $ip);
$corrected = array();
foreach ($octets as $octet) {
 array_push($corrected, (int)$octet);
}
echo implode(".", $corrected);

This will correct them:

$ip = "123.456.007.89";
$octets = explode(".", $ip);
$corrected = array();
foreach ($octets as $octet) {
 array_push($corrected, (int)$octet);
}
echo implode(".", $corrected);
隐诗 2024-12-08 03:22:35

您必须像这样接受零:

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

rubular.com 上使用此正则表达式。

我添加的 0? 匹配零次或一次出现的 0。例如, 0?[1-9][0-9] 会匹配 01010

You have to accept the zero like this:

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

Play with this regular expression on rubular.com.

The 0? I added matches zero or one occurence of 0. So 0?[1-9][0-9] for example matches both 010 and 10 for example.

琉璃繁缕 2024-12-08 03:22:35

将出现的 |1 更改为 |[01]。不过,您确定这不应该被解释为八进制数吗?一些解析器会这样做。

Change the bare |1 occurrences to |[01]. Are you sure this is not supposed to be interpreted as an octal number, though? Some resolvers do that.

蹲在坟头点根烟 2024-12-08 03:22:35

您应该弄清楚这些额外的零来自哪里。那些前导零不能被删除。在大多数平台上,它们意味着八位位组是八进制形式而不是十进制。即:八进制 064 等于十进制 52

You should figure out where those extra zeroes are coming from. Those leading zeroes can't be just dropped. On most platforms they mean that the octet is in octal form instead of decimal. That is: 064 octal equals 52 decimal.

表情可笑 2024-12-08 03:22:35

你自己也去尝试一下吗?这真的很简单。

|1[0-9][0-9]| 匹配 100-199,因为您现在想要匹配 000-199(如上所述,它是 200-155),您只需要做110 的集合。

function valid_ip($ip) {
  return preg_match("/^([1-9]|[1-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])".
          "(\.([0-9]|[1-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $ip);
}

可以将其重构(允许前导零)为:

function valid_ip($ip) {
  return preg_match("/^([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])".
          "(\.([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])){3}$/", $ip);
}

或者删除这些不需要的零:

function strip_ip($ip) {
  return preg_replace( '/0+([^0])(\.|$)/' , '$1$2' , $ip );
}

Did you have a go yourself? It's really quite simple.

|1[0-9][0-9]| matches 100-199, as you are now wanting to match 000-199 (as above that it is 200-155) you just need to make a set for the 1 to be 1 or 0.

function valid_ip($ip) {
  return preg_match("/^([1-9]|[1-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])".
          "(\.([0-9]|[1-9][0-9]|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $ip);
}

And that can be refactored down (allowing leading zeroes) to:

function valid_ip($ip) {
  return preg_match("/^([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])".
          "(\.([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])){3}$/", $ip);
}

Or to strip these unneeded zeros:

function strip_ip($ip) {
  return preg_replace( '/0+([^0])(\.|$)/' , '$1$2' , $ip );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文