Php 删除所有IP地址,除非它们以192或10开头?

Php 删除所有IP地址,除非它们以192或10开头?,php,regex,Php,Regex,以下内容将所有IP地址替换为 $match='/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/'; $replace=''; $CurrentText=preg_replace($match,$replace,$CurrentText); 如何仅替换那些不以192或10开头的IP地址 ie.10.0.0.1不应匹配,但11.0.0.1应匹配。尝试以下操作: $match = '/(?<!\d)(?!10\.|192\.)\d{1,3}(?>

以下内容将所有IP地址替换为

$match='/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/';
$replace='';
$CurrentText=preg_replace($match,$replace,$CurrentText);
如何仅替换那些不以192或10开头的IP地址

ie.10.0.0.1不应匹配,但11.0.0.1应匹配。

尝试以下操作:

$match = '/(?<!\d)(?!10\.|192\.)\d{1,3}(?>\.\d{1,3}){3}/';
$match='/(?\.\d{1,3}){3}/;
试试这个:

$match = '/(?<!\d)(?!10\.|192\.)\d{1,3}(?>\.\d{1,3}){3}/';
$match='/(?\.\d{1,3}){3}/;

专用IP地址不是这样工作的,尤其是C类专用网络192.168.0.0/16。完全有效的公共IP地址可以从192开始

使用类似以下内容:

function ip_is_private ($ip) {
    $pri_addrs = array (
                      '10.0.0.0|10.255.255.255', // single class A network
                      '172.16.0.0|172.31.255.255', // 16 contiguous class B network
                      '192.168.0.0|192.168.255.255', // 256 contiguous class C network
                      '169.254.0.0|169.254.255.255', // Link-local address also refered to as Automatic Private IP Addressing
                      '127.0.0.0|127.255.255.255' // localhost
                     );

    $long_ip = ip2long ($ip);
    if ($long_ip != -1) {

        foreach ($pri_addrs AS $pri_addr) {
            list ($start, $end) = explode('|', $pri_addr);

             // IF IS PRIVATE
             if ($long_ip >= ip2long ($start) && $long_ip <= ip2long ($end)) {
                 return true;
             }
        }
    }

    return false;
}
函数ip是私有的($ip){
$pri_addrs=数组(
'10.0.0.0 | 10.255.255.255',//单一A类网络
“172.16.0.0 | 172.31.255.255”,//16连续B类网络
“192.168.0.0 | 192.168.255.255”,//256连续C类网络
‘169.254.0.0 | 169.254.255.255’,//链接本地地址也称为自动专用IP地址
'127.0.0.0 | 127.255.255.255'//localhost
);
$long_ip=ip2long($ip);
如果($long_ip!=-1){
foreach($pri_addr作为$pri_addr){
列表($start,$end)=分解('|',$pri_addr);
//如果是私人的

如果($long_ip>=ip2long($start)&&$long_ip专用ip地址不是这样工作的,特别是C类专用网络192.168.0.0/16。完全有效的公用ip地址可以从192开始

使用类似以下内容:

function ip_is_private ($ip) {
    $pri_addrs = array (
                      '10.0.0.0|10.255.255.255', // single class A network
                      '172.16.0.0|172.31.255.255', // 16 contiguous class B network
                      '192.168.0.0|192.168.255.255', // 256 contiguous class C network
                      '169.254.0.0|169.254.255.255', // Link-local address also refered to as Automatic Private IP Addressing
                      '127.0.0.0|127.255.255.255' // localhost
                     );

    $long_ip = ip2long ($ip);
    if ($long_ip != -1) {

        foreach ($pri_addrs AS $pri_addr) {
            list ($start, $end) = explode('|', $pri_addr);

             // IF IS PRIVATE
             if ($long_ip >= ip2long ($start) && $long_ip <= ip2long ($end)) {
                 return true;
             }
        }
    }

    return false;
}
函数ip是私有的($ip){
$pri_addrs=数组(
'10.0.0.0 | 10.255.255.255',//单一A类网络
“172.16.0.0 | 172.31.255.255”,//16连续B类网络
“192.168.0.0 | 192.168.255.255”,//256连续C类网络
‘169.254.0.0 | 169.254.255.255’,//链接本地地址也称为自动专用IP地址
'127.0.0.0 | 127.255.255.255'//localhost
);
$long_ip=ip2long($ip);
如果($long_ip!=-1){
foreach($pri_addr作为$pri_addr){
列表($start,$end)=分解('|',$pri_addr);
//如果是私人的
如果($long\u ip>=ip2long($start)&&$long\u ip
将它们与
ip2long()


将它们与
ip2long()
$match[0]
组合使用,以实现更好的命中测试。

使用一些奇特的
preg\u replace\u callback()
使其更易于理解:

$ip = "192.168.1.1";
$match = '/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/';
$replace = '<ip>';

$ip = preg_replace_callback($match, function($matches) use ($replace) { //Run this function every time there's a match. Bring $replace from the outside.
    $first = array_shift(explode(".", $matches[0])); //Get first segment of the IP
    if (!in_array($first, ["192", "10"])) { //If the segment isn't 192 or 10
        return $replace; //Return the replacement (<ip>)
    }
    return $matches[0]; //Else, return the IP unchanged.
}, $ip);

echo $ip;
$ip=“192.168.1.1”;
$match='/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/';
$replace='';
$ip=preg\u replace\u回调($match,function($matches)使用($replace){//每次有匹配时都运行此函数。从外部带$replace。
$first=array_shift(分解(“.”,$matches[0]);//获取IP的第一段
如果(!in_数组($first,[“192”,“10”]){//如果段不是192或10
return$replace;//返回替换项()
}
return$matches[0];//否则,返回IP不变。
}(港币),;
echo$ip;

使用一些花哨的
preg\u replace\u callback()
使其更易于理解:

$ip = "192.168.1.1";
$match = '/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/';
$replace = '<ip>';

$ip = preg_replace_callback($match, function($matches) use ($replace) { //Run this function every time there's a match. Bring $replace from the outside.
    $first = array_shift(explode(".", $matches[0])); //Get first segment of the IP
    if (!in_array($first, ["192", "10"])) { //If the segment isn't 192 or 10
        return $replace; //Return the replacement (<ip>)
    }
    return $matches[0]; //Else, return the IP unchanged.
}, $ip);

echo $ip;
$ip=“192.168.1.1”;
$match='/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/';
$replace='';
$ip=preg\u replace\u回调($match,function($matches)使用($replace){//每次有匹配时都运行此函数。从外部带$replace。
$first=array_shift(分解(“.”,$matches[0]);//获取IP的第一段
如果(!in_数组($first,[“192”,“10”]){//如果段不是192或10
return$replace;//返回替换项()
}
return$matches[0];//否则,返回IP不变。
}(港币),;
echo$ip;

添加到你的帖子中,并把它标记为正确的答案。请接受编辑。编辑:不顾,我的修复不好,你的建议仍然无效。我添加了一个查找表,以确保我们不在一个数字中间开始匹配。添加一个修正到你的帖子并标记它正确的答案。请接受E。编辑:不顾,我的修复是坏的,你的建议仍然没有用。我增加了一个查找表,以确保我们不开始匹配在一个数字的中间。