Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Determine if the URL contains a domain.
- * $domains : list of domains (an array or a string separated by semicolons)
- * $remove : list of subdomains to remove (or TRUE/FALSE to remove all/none)
- */
- function match_domain($url, $domains)
- {
- $url = $this->extract_host($url);
- $url = utf8_case_fold_nfc($url);
- $url_split = array_reverse(explode('.', $url));
- $domain_list = is_string($domains) ? explode(';', $domains) : $domains;
- foreach ($domain_list as $domain)
- {
- $domain = $this->extract_host($domain);
- $domain = utf8_case_fold_nfc($domain);
- // Ignoring all subdomains, so check if our URL ends with domain
- if (substr($url, -strlen($domain)) == $domain)
- {
- return true;
- }
- $domain_split = array_reverse(explode('.', $domain));
- $match_count = 0;
- $match_list = array();
- foreach ($domain_split as $index => $segment)
- {
- if (isset($url_split[$index]) && strcmp($url_split[$index], $segment) === 0)
- {
- $match_count += 1;
- array_splice($match_list, 0, 0, $segment);
- continue;
- }
- break;
- }
- if ($match_count > 2 || ($match_count == 2 && strlen($match_list[0]) > 2)) // not the best check, but catches domains like 'co.jp'
- {
- return true;
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement