note 24956 added to function.preg-match-all
| From: | antony dot booth at kill dot webbots dot com | Date: | Thu, 05 Sep 2002 23:54:25 +0000 |
| Subject: | note 24956 added to function.preg-match-all | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-36213@lists.php.net to get a copy of this message | ||
The example to extract the domain from the URL is only good for a 2 part domain name (.company.com
etc) but for 3 or more, it is not useful ( company.co.uk etc would return .co.uk).
I found this more generic: -
<?php
//Get the web site address
preg_match("/^(http:\/\/)?([^\/]+)/i", "http://www.multi.part.domain.name.com/index.html",
$matches);
$host = $matches[2];
// get all but one segments of host name
preg_match_all("/\.([^\.\/]+)/",$host,$matches);
// first element does not want the period('.')
$matches[0][0] = $matches[1][0];
$domain = implode('',$matches[0]);
echo( "Domain: " . $domain);
?>
Displays: -
Domain: multi.part.domain.name.com
--
http://www.php.net/manual/en/function.preg-match-all.php
http://master.php.net/manage/user-notes.php?action=edit+24956
http://master.php.net/manage/user-notes.php?action=delete+24956
http://master.php.net/manage/user-notes.php?action=reject+24956