note 33868 added to function.preg-match
| From: | ewspencer at industrex dot com | Date: | Wed, 09 Jul 2003 18:20:06 +0000 |
| Subject: | note 33868 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51802@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33868
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33868&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33868&report=yes