如何删除 IP 上的 CIDR 表示法?

发布于 2024-12-28 20:15:59 字数 167 浏览 5 评论 0原文

我想摆脱 CIDR 表示法并尝试了以下方法,但它似乎不起作用:

<?php
  $txt='156.67.0.0/16';
  $re='(\\/)'.'(\\d+)';

  $end = rtrim($txt,$re);
  echo $end;
?>

I want to get rid of the CIDR notation and tried the following, but it doesn't seem to work like this:

<?php
  $txt='156.67.0.0/16';
  $re='(\\/)'.'(\\d+)';

  $end = rtrim($txt,$re);
  echo $end;
?>

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

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

发布评论

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

评论(4

手长情犹 2025-01-04 20:15:59

trim() 不接受正则表达式,但接受字符列表。您可以简单地拆分字符串并仅使用第一部分:

$parts = explode('/', $str);
echo $parts[0];

trim() doesn't accept a regex but a caracter list. You can simply split the string and only use the first part though:

$parts = explode('/', $str);
echo $parts[0];
提笔落墨 2025-01-04 20:15:59

使用 preg_replace

preg_replace('~/.*~', '', $txt);

这会删除从削减。

Use preg_replace:

preg_replace('~/.*~', '', $txt);

This removes everything starting the slash.

宣告ˉ结束 2025-01-04 20:15:59

rtrim 接受字符列表,而不是正则表达式。查看preg_replace

$end = preg_replace('@/.*$@', '', $txt);

rtrim accepts a character list, not a regular expression. Look into preg_replace.

$end = preg_replace('@/.*$@', '', $txt);
江城子 2025-01-04 20:15:59

我会使用 preg_replace()

$ip = '156.67.0.0/16';
$ip = preg_replace('#/\d+$#', '', $ip);

echo $ip; // 156.67.0.0

I would use preg_replace():

$ip = '156.67.0.0/16';
$ip = preg_replace('#/\d+$#', '', $ip);

echo $ip; // 156.67.0.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文