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
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";
}

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.