I took notice of this thread and thought it would be a nice little exercise to take a URL and break it up into parts (including sub domains).
Now I immediately started thinking of simply using parse_url and use ['host'] to get the domain. But if one wants explicit detailed sub domains within what is returned from ['host'], it seems more work is needed. So I came up with this script. I would like to know how to make this more streamlined / elegant / efficient (yet display the results you see here).
So here is the code:
function urlParser($url){
$urlHost = parse_url($url);
echo 'url - ' . $urlHost['host'] . '<br />';
preg_match('#(?:\.\w{2}\.\w{2})|(?:\.\w{2,})$#', $urlHost['host'], $match); // $match[0] = Top-level Domain
$urlRevised = substr($urlHost['host'], 0, strlen($urlHost['host']) - strlen($match[0]));
$urlArray = explode('.', $urlRevised);
array_shift($urlArray); // eliminate $url[0] = 'www.'
//////////////////
// display info //
//////////////////
if(count($urlArray) > 1){
for($i = 0, $x =1, $count = count($urlArray) -1; $i < $count; $i++){
echo 'Sub domain ' . $x++ . ': ' . $urlArray[$i] . '<br />';
}
echo 'Domain : ' . end($urlArray) . $match[0] . '<br />' . '-------------------------------------------------------------' . '<br />';
} else {
echo 'Domain : ' . $urlArray[0] . $match[0] . '<br />' . '-------------------------------------------------------------' . '<br />';
}
}
$str = array('http://www.example.ca', 'http://www.subdomain1.phpbuilder.com/board/', 'http://www.subdomain1.subdomain2.subdomain3.example.co.uk/images/gif/');
foreach($str as $url){
urlParser($url);
}
Output:
url - www.example.ca
Domain : example.ca
-------------------------------------------------------------
url - www.subdomain1.phpbuilder.com
Sub domain 1: subdomain1
Domain : phpbuilder.com
-------------------------------------------------------------
url - www.subdomain1.subdomain2.subdomain3.example.co.uk
Sub domain 1: subdomain1
Sub domain 2: subdomain2
Sub domain 3: subdomain3
Domain : example.co.uk
-------------------------------------------------------------
Ok, so here is one thought process design snag I ran into.. notice the preg aspect? I was at first trying to devise a way to detect between formats ending in '.xx.xx' (think .co.uk), or every other format '.xx or .xxx or .xxxx, etc...) but capture everything else and use this as the basis for further calculations.. I could only get either the '.xx.xx' format with the rest as captured or the '.xx / .xxx / .xxxx' format with the rest as captured, but not both.. so I used $match[0] (which does successfully find the ending in either format) and subtracted this length from the length of the full URL and went from there.
So the code does work. But again, I sense the function urlParser() can be streamlined / more elegant / effective. I would be very interested in seeing the feedback from all this.
Time to make a fool of myself (again) 😉
Cheers,
NRG