Jump to content

how to get the site name from domain ?


jjk2

Recommended Posts

lets say there are large variety of domain names

 

www.asdf.com

xpics.jos.com

ps1.ccc.us

aaa.info

 

my problem is, how can i get the site name, or the name between the two "."

 

so i can get asdf, from www.asdf.com

 

what about in cases where only aaa.info ?

 

i'm totally stumped on this one, i would greatly appreciate it if someone could help me!

Link to comment
https://forums.phpfreaks.com/topic/152628-how-to-get-the-site-name-from-domain/
Share on other sites

premiso, in the case of these urls, $siteName['host'] is equal to the entire url. Given that the OP wants the content between the dots, I would use pathinfo - (the link is for the OP, in case he/she isn't aware of that function).

 

So since the urls don't contain additional info like file names, it is safe to make use of ['filename'] from pathinfo:

 

$arr = array('www.asdf.com','xpics.jos.com','ps1.ccc.us','aaa.info');
foreach ($arr as $val) {
$path = pathinfo($val);
preg_match('#^(?:[^.]+\.)?(.*)#', $path['filename'], $match);
echo $match[1] , "<br />\n";
}

Or if one prefers a non-regex way using the gist of the above method:

 

$arr = array('www.asdf.com','xpics.jos.com','ps1.ccc.us','aaa.info');
foreach ($arr as $val) {
$path = pathinfo($val);
echo (strstr($path['filename'], '.'))? substr($path['filename'], (strpos($path['filename'], '.'))+1 ) . "<br />\n" : $path['filename'] . "<br />\n";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.