将ip的最后两个八位字节替换为* PHP


replace last two octets of an ip with * PHP

我希望能够用*'s替换最后两个八位之间的数字

127.0.0.1变为127.0.*.*88.88.88.88变为88.88.**.**

我该怎么做?

您可以使用explode以分隔符分隔字符串:

$myIP = "127.0.0.1";
$ipOctets = explode('.', $myIP);
$newIp = $ipOctets[0] . '.' . $ipOctets[1] . '.' . preg_replace('/./', '*', $ipOctets[4]) . '.' . preg_replace('/./', '*', $ipOctets[3]);

作为提示,尝试将IP地址分解并重新构建为四个单独的项。

echo substr($ip, 0, strrpos($ip, '.')).'.***';