It is currently Tue May 21, 2013 1:04 am

All times are UTC


You can access below Applications



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: function for returning domain name from url in php
PostPosted: Wed Apr 28, 2010 8:00 am 
Offline
User avatar

Joined: Tue Mar 02, 2010 10:49 am
Posts: 171
Location: India/Tamilnadu/Tirunelveli
Below the short php function to parse a URL and return just the domain name without using regex.

function getDomain($url)
{
$exten="";
$explode = explode(".", $url);
$firstpart = $explode[0];
$secondpart = $explode[1];
if(!strstr($url,"www")) // for url have www
{
$secondpart=explode("/",$secondpart);
$secondpart=$secondpart[0];
}
else // not have www
{
$thirdpart = $explode[2];
$thirdpart = explode("/", $thirdpart);
$exten = ".".$thirdpart[0];
}

return $firstpart.".".$secondpart.$exten;
}

$domain=getdomain("http://www.qualitypointtech.net/NewsFeed/view_allfeed.php");
$domain1=getdomain("http://qualitypointtech.net/NewsFeed/view_allfeed.php");

the first one return the 'http://www.qualitypointtech.net' and second one return the 'http://qualitypointtech.net' as output.

so we need not worry about whether the url have www or not.
Share |

_________________
Anbarasan k


Top
 Profile  
 
 Post subject: get host name from URL in php
PostPosted: Mon Feb 28, 2011 11:06 am 
Offline
User avatar

Joined: Tue Mar 02, 2010 10:49 am
Posts: 171
Location: India/Tamilnadu/Tirunelveli
In the above code it return domain name with http:// and http://www.

I needed only host name like this, 'qualitypointtech.net' if my input is anything. I used below code getting this.

Code:
// get host name from URL
function getHostName($Address)
{
    
   $parseUrl 
= parse_url(trim($Address)); 
   $url 
= trim($parseUrl['host'] ? $parseUrl['host'] : array_shift(explode('/', $parseUrl['path'], 2)));
   if(strstr($url,"www"))
   {
       return $url = substr($url,4);
   }
   else
   
{
       return $url;
   }
}
 


Code:
getHostName('http://qualitypointtech.net/NewsFeed/view_allfeed.php'); // it return qualitypointtech.net
getHostName('http://www.qualitypointtech.net/NewsFeed/view_allfeed.php'); // it return qualitypointtech.net
getHostName('qualitypointtech.net'); // it return qualitypointtech.net
getHostName('http://qualitypoint.blogspot.com/'); // return qualitypoint.blogspot.com 
Share |

_________________
Anbarasan k


Top
 Profile  
 
 Post subject: Re: function for returning domain name from url in php
PostPosted: Fri Apr 27, 2012 7:31 am 
Offline

Joined: Sat Mar 10, 2012 12:03 pm
Posts: 39
While executing the above code we got the error message as getHostName() in line23

This error message will display, based on the server settings. So to resolve this problem we have changed the function name now it was working fine.
Share |


Top
 Profile  
 
 Post subject: Re: function for returning domain name from url in php
PostPosted: Wed May 09, 2012 10:06 am 
Offline
User avatar

Joined: Tue Mar 02, 2010 10:49 am
Posts: 171
Location: India/Tamilnadu/Tirunelveli
Thanks for Your correction.
Share |

_________________
Anbarasan k


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron