Click to See Complete Forum and Search --> : REGEX - Parse domain from referring URL


kouki0196
02-09-2005, 03:25 PM
Hi there,

I am looking for a REGEX that will help me parse a URL and extract just the domain. The pattern would be

//0 [http:// or https://] +
//1 [www or subdomain].[?subdomain].domain.[TLD][?:port] +
//2 [path (any characters ... doesn't matter]

The returned match would be //1

Thanks a bunch!

NogDog
02-09-2005, 04:11 PM
<?php
$string = "http://this.is.atest.com/dir/subdir/file.html";
preg_match("/https{0,1}:\\/\\/([^\\/]*)\\/*.*/i", $string, $matches);
echo "Domain: '{$matches[1]}'";
?>

kouki0196
02-09-2005, 04:23 PM
EXACTLY!!! Thank you!

ShrineDesigns
02-09-2005, 04:30 PM
try this:<?php
if(preg_match("/^[htfps]{3,}\\:\\/\\/(\\w+)?\\.?(\\w{3,})\\.(\\w{3,}|\\w+\.\\w+)/i", "http://www.google.jp.com/search", $matches))
{
array_shift($matches);
//print_r($matches);
echo implode('.', $matches);
}
?>