我希望能够用*'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, '.')).'.***';