【问题标题】:Remove all IP addresses unless they start with 192 or 10?删除所有 IP 地址,除非它们以 192 或 10 开头?
【发布时间】:2014-03-31 17:32:17
【问题描述】:

以下将任何 IP 地址替换为 <ip>

$match = '/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/';
$replace = '<ip>';
$CurrentText = preg_replace($match, $replace, $CurrentText);

如何只替换以 192 或 10 开头的 IP 地址。

即。 10.0.0.1 不应该匹配,但 11.0.0.1 应该匹配。

【问题讨论】:

    标签: php regex


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 添加为您的帖子添加了更正并将其标记为正确答案。请接受编辑。编辑:忽略,我的修复很糟糕,你的建议仍然不起作用。
    • 我添加了一个lookbehind以确保我们不会在数字中间开始匹配。
    【解决方案2】:

    私有 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;
    }
    

    发件人:https://stackoverflow.com/a/13818126/1064767

    【讨论】:

      【解决方案3】:
      // test string
      $string = '192.168.1.1,10.0.0.1,1.2.3.4,BAD:256.257.258.259';
      // first must be between 1-255
      $npf = '([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])';
      // next must be between 0-255
      $npn = '([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])';
      // ip has to be bordered by \b regex to be valid
      $pattern = "~\\b{$npf}\\.{$npn}\\.{$npn}\\.{$npn}\\b~s";
      // replace with a callback
      $string = preg_replace_callback($pattern, function($match){
          // play with them as you wish
          $ip = $match[0];
          $match1 = intval($match[1]);
          if(in_array($match1, array(10, 192))){
              return $ip;
          }
          return '<ip>';
      }, $string);
      // clean up
      unset($npf, $npn);
      // output string
      var_dump($string);
      

      代码已注释,应该有意义。

      奖金

      这些是私有 IP 范围。

      $private_ranges = array(
          // 10.0.0.1 to 10.255.255.254
          ip2long('10.0.0.0') => ip2long('10.255.255.255'),
          // 172.16.0.1 to 172.31.255.254
          ip2long('172.16.0.0') => ip2long('172.31.255.255'),
          // 192.168.0.1 to 192.168.255.254
          ip2long('192.168.0.0') => ip2long('192.168.255.255'),
           // 127.0.0.0 to 127.255.255.255
          ip2long('127.0.0.0') => ip2long('127.255.255.255'),
      );
      

      ip2long()$match[0] 的数组中使用它们,以获得更好的命中测试。

      【讨论】:

        【解决方案4】:

        加上一些花哨的preg_replace_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;
        

        【讨论】:

          猜你喜欢
          • 2017-10-03
          • 2017-01-24
          • 2021-11-12
          • 1970-01-01
          • 1970-01-01
          • 2015-01-18
          • 2021-03-26
          • 2017-05-25
          • 2014-11-30
          相关资源
          最近更新 更多