Advertisement
geolim4

const

Oct 20th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | None | 0 0
  1.         /**
  2.         * Determine if the URL contains a domain.
  3.         * $domains  : list of domains (an array or a string separated by semicolons)
  4.         * $remove   : list of subdomains to remove (or TRUE/FALSE to remove all/none)
  5.         */
  6.         function match_domain($url, $domains)
  7.         {
  8.             $url = $this->extract_host($url);
  9.             $url = utf8_case_fold_nfc($url);
  10.             $url_split = array_reverse(explode('.', $url));
  11.  
  12.             $domain_list = is_string($domains) ? explode(';', $domains) : $domains;
  13.             foreach ($domain_list as $domain)
  14.             {
  15.                 $domain = $this->extract_host($domain);
  16.                 $domain = utf8_case_fold_nfc($domain);
  17.  
  18.                 // Ignoring all subdomains, so check if our URL ends with domain
  19.                 if (substr($url, -strlen($domain)) == $domain)
  20.                 {
  21.                     return true;
  22.                 }
  23.                 $domain_split = array_reverse(explode('.', $domain));
  24.                 $match_count = 0;
  25.                 $match_list = array();
  26.                 foreach ($domain_split as $index => $segment)
  27.                 {
  28.                     if (isset($url_split[$index]) && strcmp($url_split[$index], $segment) === 0)
  29.                     {
  30.                         $match_count += 1;
  31.                         array_splice($match_list, 0, 0, $segment);
  32.                         continue;
  33.                     }
  34.                     break;
  35.                 }
  36.                 if ($match_count > 2 || ($match_count == 2 && strlen($match_list[0]) > 2)) // not the best check, but catches domains like 'co.jp'
  37.  
  38.  
  39.  
  40.                 {
  41.                     return true;
  42.                 }
  43.             }
  44.             return false;
  45.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement