Menu
Folks,
One look at google or StackOverFlow and programmers come with variety of ways to extract a domain from a url. I’m spoilt for choice. Need to fool-proof one. How-about we checkout the code you use yourselves ? as of me, like I said. Spoilt for choice and confused.
parse_url()
$_SERVER['SERVER_NAME']
<i>
</i><?php
$url = 'http://canine.dogs.nogdog.com/index.php?search=alsation';
$domain = parse_url($url);
echo $domain['host'];
?>
<i>
</i><?php
$url = 'http://canine.dogs.ginerjm.com/index.php?search=alsation';
$domain = parse_url($url);
echo $domain['host']; //canine.dogs.ginerjm.com
?>
<i>
</i><?php
$url = 'http://canine.dogs.ginerjm.com/index.php';
$domain = parse_url($url);
echo $domain['host']; //canine.dogs.ginerjm.com
?>
<i>
</i><?php
$url = 'http://www.ginerjm.com/index.php?search=alsation';
$domain = parse_url($url);
echo $domain['host']; //www.ginerjm.com
?>
www.example.co.uk
"co.uk"
www
foo.example.com
bar.example.com
foo.bar.example.com
explode()
array_slice()
implode()
<i>
</i><?php
$url = 'http://www.nogdog.com/?search=cars';
$parse_url = parse_url($url);
$domain = $parse_url['host'];
$level_domains = explode('.',$domain);
print_r($level_domains);
$level_domains_count = count($level_domains);
$array_pos = $level_domains_count - 2;
echo $level_domains[$array_pos];
?>
<i>
</i><?php
$url = 'http://www.nogdog.co.uk/?search=cars';
$parse_url = parse_url($url);
$domain = $parse_url['host'];
$level_domains = explode('.',$domain);
print_r($level_domains);
$level_domains_count = count($level_domains);
$array_pos = $level_domains_count - 2;
echo $level_domains[$array_pos];
?>
[code=php]
$user_domain = array_pop(explode('@', $user_email));
$regex = '/(^|.)' . preg_quote($user_domain) . '$/i';
$submitted_domain = parse_url($submitted_url, PHP_URL_HOST)
if(!preg_match($regex, $submitted_domain)) {
// not allowed
}
[/code]
0.1.9 — BETA 5.2