title: check if an ip matches any ip contained in an ip list using php
body: <p>in array $white I have an ip list like this (about 10000-12000 ips)</p>
  
  <pre><code>74.125.244.0/22
  207.126.144.0/20
  77.238.189. 
  212.211.149.128/26
  194.25.134.8 
  194.25.134.9 
  194.25.134.12       
  174.2.114.12-174.2.115.255
  153.2.242.243
  153.2.244.12 
  153.2.244.50 
  153.2.244.63 
  153.2.246.30 
  153.2.246.31 
  153.2.246.35 
  153.2.247.30 
  153.2.247.31 
  153.2.247.32      
  </code></pre>
  
  <p>So, the ip list in array $white can contain ip(s) in these three format</p>
  
  <pre><code>153.2.247.32 (simple ip address)
  207.126.144.0/20 ( CIDR notation )
  174.2.114.12-174.2.115.255 ( Hyphenated ranges )
  </code></pre>
  
  <p>My goal is to check if ips listed in another array , $ips (about 1000-2000 ips) , are present in
  the $white list.</p>
  
  <p>The format in the $ips list is only simple ip address like</p>
  
  <pre><code>207.126.144.0
  207.126.141.2
  201.126.144.5
  </code></pre>
  
  <p>I am doing this</p>
  
  <pre><code>foreach ($ips as $check) {
  if (in_array($check,$white)){
  echo "Ip $check exists";
  }
  }
  </code></pre>
  
  <p>However it's only good to check simple ip addresses , but I cannot check if the ips listed in $ips are contained in the Hyphenated ranges and CIDR ranges of $white .</p>
  
  <p>I found a solution using ip_in_range.php
  <a href="https://github.com/irazasyed/php-ip-range/blob/master/ip_in_range.php" rel="nofollow noreferrer">https://github.com/irazasyed/php-ip-range/blob/master/ip_in_range.php</a></p>
  
  <pre><code>foreach ($ips as $check) {
      if (in_array($check,$white))
      {
      echo "Ip $check exists";
      }
      else
      {
      foreach( $white as $checkrange )
      {       
  if (substr_count($checkrange, '/')&gt;=1 or substr_count($checkrange, '-')&gt;=1 )
  { 
      if (ip_in_range($check, $checkrange)) 
  { 
      echo "Ip $check exists"; 
      break;
  }
  }
  
  }
  }
  }
  </code></pre>
  
  <p>But it's very slow , because $white and $ips are huge lists .
  Does exist a faster and more efficient solution ?</p>