note 33868 deleted from function.preg-match by sniper
| From: | sniper@php.net | Date: | Fri, 17 Oct 2003 03:52:38 +0000 |
| Subject: | note 33868 deleted from function.preg-match by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58735@lists.php.net to get a copy of this message | ||
Note Submitter: ewspencer@industrex.com
----
Example 3 above works well, in fact more reliably than parse_url(). I adapted the example code
within in the following function to accommodate domain names that may include a country code top
level domain (cctld).
function getdomain($url)
{
// patterns we need to match
$pattern_hostname = '/^(http:\/\/)?([^\/]+)/i';
$pattern_domain = '/[^\.\/]+\.[^\.\/]+(\.[^\.\/]{2})?$/';
// extract hostname from URL string
preg_match($pattern_hostname, $url, $matches);
$hostname = $matches[2];
// extract sld.tld(.cctld if exists)
preg_match($pattern_domain, $hostname, $matches);
$domain = $matches[0];
return $domain;
}
-- Erich