note 104874 added to function.parse-url
| From: | gustavo dot andriuolo at vulcabras dot com dot ar | Date: | Wed, 13 Jul 2011 01:31:30 +0000 |
| Subject: | note 104874 added to function.parse-url | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181450@lists.php.net to get a copy of this message | ||
Here's a method to get the REAL name of a domain. This return just the domain name, not the
rest. First check if is not an IP, then return the name:
<?php
function esip($ip_addr)
{
//first of all the format of the ip address is matched
if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$ip_addr))
{
//now all the intger values are separated
$parts=explode(".",$ip_addr);
//now we need to check each part can range from 0-255
foreach($parts as $ip_parts)
{
if(intval($ip_parts)>255 || intval($ip_parts)<0)
return FALSE; //if number is not within range of 0-255
}
return TRUE;
}
else
return FALSE; //if format of ip address doesn't matches
}
function domain($domainb)
{
$bits = explode('/', $domainb);
if ($bits[0]=='http:' || $bits[0]=='https:')
{
$domainb= $bits[2];
} else {
$domainb= $bits[0];
}
unset($bits);
$bits = explode('.', $domainb);
$idz=count($bits);
$idz-=3;
if (strlen($bits[($idz+2)])==2) {
$url=$bits[$idz].'.'.$bits[($idz+1)].'.'.$bits[($idz+2)];
} else if (strlen($bits[($idz+2)])==0) {
$url=$bits[($idz)].'.'.$bits[($idz+1)];
} else {
$url=$bits[($idz+1)].'.'.$bits[($idz+2)];
}
return $url;
}
$address='clients1.sub3.google.co.uk';
$parsed_url = parse_url($address);
$check = esip($parsed_url['host']);
$host = $parsed_url['host'];
if ($check == FALSE){
if ($host != ""){
$host = domain($host);
}else{
$host = domain($address);
}
}
echo $host;
?>
This return: google.co.uk
'http://sub1.sub2.sub3.loquesea.com.ar:443';
return: loquesea.com.ar
'loquesea.com.ar';
return: loquesea.com.ar
Many times parse_url return nothing when domain is google.com only for example.
Now, google.com, or google.co.uk will return the same.
Maybe is a little dirty, but works well for me, i use it to group internet access log from squid.
Regards.
----
Server IP: 69.147.83.197
Probable Submitter: 186.60.9.60
----
Manual Page -- http://www.php.net/manual/en/function.parse-url.php
Edit -- https://master.php.net/note/edit/104874
Del: integrated -- https://master.php.net/note/delete/104874/integrated
Del: useless -- https://master.php.net/note/delete/104874/useless
Del: bad code -- https://master.php.net/note/delete/104874/bad+code
Del: spam -- https://master.php.net/note/delete/104874/spam
Del: non-english -- https://master.php.net/note/delete/104874/non-english
Del: in docs -- https://master.php.net/note/delete/104874/in+docs
Del: other reasons-- https://master.php.net/note/delete/104874
Reject -- https://master.php.net/note/reject/104874
Search -- https://master.php.net/manage/user-notes.php