note 104874 modified in function.parse-url by danbrown
| From: | danbrown@php.net | Date: | Wed, 13 Jul 2011 17:01:40 +0000 |
| Subject: | note 104874 modified in function.parse-url by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181473@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.example.com:443';
return: example.com
'loquesea.org';
return: loquesea.org
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.
--was--
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.
http://php.net/manual/en/function.parse-url.php