jjk2 Posted April 5, 2009 Share Posted April 5, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/152628-how-to-get-the-site-name-from-domain/ Share on other sites More sharing options...
premiso Posted April 6, 2009 Share Posted April 6, 2009 parse_url $url = "www.asdf.com"; $siteName = parse_url("http://{$url}"); $siteName = $siteName['host']; echo $siteName; Quote Link to comment https://forums.phpfreaks.com/topic/152628-how-to-get-the-site-name-from-domain/#findComment-802515 Share on other sites More sharing options...
nrg_alpha Posted April 6, 2009 Share Posted April 6, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/152628-how-to-get-the-site-name-from-domain/#findComment-802596 Share on other sites More sharing options...
nrg_alpha Posted April 6, 2009 Share Posted April 6, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/152628-how-to-get-the-site-name-from-domain/#findComment-802608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.