我需要匿名化IPv4和IPv6地址,因此我编写了这个粗略的解决方案:
if (strlen($_SERVER['REMOTE_ADDR']) <= 15) { // Sorry: 15 NOT 12
echo htmlentities(substr_replace($_SERVER['REMOTE_ADDR'], 'XXX', -3), ENT_QUOTES);
} else {
echo htmlentities(substr_replace($_SERVER['REMOTE_ADDR'], 'XXXX:XXXX', -9), ENT_QUOTES);
}
它适用于全长IPv4和IPv6地址,如
207.142.131.005
2001:0db8:0000:08d3:0000:8a2e:0070:7344
但不是缩写地址,如
207.142.131.5
2001:0db8 :: 8d3 :: 8a2e:7:7344
我想知道是否有一个优雅的preg_replace解决方案和一些正则表达式魔法?
答案 0 :(得分:3)
无需任何条件。您可以为单个preg_replace()
调用处理编写两个模式和两个替换项。
代码:(Demo)
$tests=[
"207.142.131.005",
"2001:0db8:0000:08d3:0000:8a2e:0070:7344",
"2001:0db8:0000:08d3:0000:8a2e:0070:734a",
"207.142.131.5",
"2001:0db8::8d3::8a2e:7:7344",
"::1",
"127.0.0.1"
];
$tests=preg_replace(['/\.\d*$/','/[\da-f]*:[\da-f]*$/'],['.XXX','XXXX:XXXX'],$tests);
var_export($tests);
输出:
array (
0 => '207.142.131.XXX',
1 => '2001:0db8:0000:08d3:0000:8a2e:XXXX:XXXX',
2 => '2001:0db8:0000:08d3:0000:8a2e:XXXX:XXXX',
3 => '207.142.131.XXX',
4 => '2001:0db8::8d3::8a2e:XXXX:XXXX',
5 => ':XXXX:XXXX',
6 => '127.0.0.XXX',
)
模式说明:
的IPv4:
/ #Pattern delimiter
\. #Match dot literally
\d* #Match zero or more digits
$ #Match the end of the string
/ #Pattern delimiter
的IPv6
/ #Pattern delimiter
[\da-f]* #Match zero or more digits or a b c d e f
: #Match colon
[\da-f]* #Match zero or more digits or a b c d e f
$ #Match the end of the string
/ #Pattern delimiter
答案 1 :(得分:1)
不使用正则表达式,您可以相应地.
或:
进行爆炸并替换最后两个部分:
<?php
function anonIp($ip)
{
if (strpos($ip, ".") !== false) { // detect IP type by dots instead of length
$pieces = explode(".", $ip);
$nPieces = count($pieces);
$pieces[$nPieces - 1] = $pieces[$nPieces - 2] = "XXX";
return implode(".", $pieces);
} else {
$pieces = explode(":", $ip);
$nPieces = count($pieces);
$pieces[$nPieces - 1] = $pieces[$nPieces - 2] = "XXXX";
return implode(":", $pieces);
}
}
var_dump(anonIp("207.142.131.005")); // 207.142.XXX.XXX
var_dump(anonIp("2001:0db8:0000:08d3:0000:8a2e:0070:7344")); // 2001:0db8:0000:08d3:0000:8a2e:XXXX:XXXX
var_dump(anonIp("207.142.131.5")); // 207.142.XXX.XXX
var_dump(anonIp("2001:0db8::8d3::8a2e:7:7344")); // 2001:0db8::8d3::8a2e:XXXX:XXXX
var_dump(anonIp("::1")); // :XXXX:XXXX
var_dump(anonIp("127.0.0.1")); // 127.0.XXX.XXX
虽然我确定有一些不明显(或没有那么多)的情况会破坏,所以一定要先给它一个彻底的测试。
答案 2 :(得分:0)
您可以尝试:
$string = $_SERVER['REMOTE_ADDR'];
if (strlen($string) <= 12) {
echo htmlentities(preg_replace('/(\d+\.\d+.\d+.)(\d+)/', "$1XXX", $string), ENT_QUOTES);
} else {
echo htmlentities(preg_replace('/((?:[0-9a-f]+:){6})(\w+:\w+)/', "$1XXXX:XXXX", $string), ENT_QUOTES);
}
答案 3 :(得分:0)
正则表达式:[0-9]+$
and [0-9]*:[0-9]+$
或\d+$
和\d*:\d+$
详细说明:
$
断言位于行尾的位置[]
匹配列表中的单个字符+
匹配一次且无限次*
零和无限次之间的匹配PHP代码:
function mask($ip)
{
if (strpos($ip, ".") == true) {
print_r(preg_replace('~[0-9]+$~', 'XXX', $ip) . "\n");
} else {
print_r(preg_replace('~[0-9]*:[0-9]+$~', 'XXXX:XXXX', $ip) . "\n");
}
}
输出:
207.142.131.XXX
207.142.131.XXX
2001:0db8:0000:08d3:0000:8a2e:XXXX:XXXX
2001:0db8::8d3::8a2e:XXXX:XXXX
:XXXX:XXXX