RE: [PHP] Parse domain from URL

This is a discussion on RE: [PHP] Parse domain from URL within the PHP General forums, part of the PHP Programming Forums category; > From: Brad Fuller [mailto:bfuller@cpacampaigns.com] > Sent: Wednesday, June 06, 2007 5:44 PM > Subject: [php] ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-07-2007
Stefan Wixfort
 
Posts: n/a
Default RE: [PHP] Parse domain from URL


> From: Brad Fuller [mailto:bfuller@cpacampaigns.com]
> Sent: Wednesday, June 06, 2007 5:44 PM
> Subject: [php] Parse domain from URL
>
> Hey guys,
>
> I'm faced with an interesting problem, and wondering if there's an easy
> solution.
>
> I need to strip out a domain name from a URL, and ignore subdomains (like
> www)
>
> I can use parse_url to get the hostname. And my first thought was to take
> the last 2 segments of the hostname to get the domain. So if the URL is
> http://www.example.com/
> Then the domain is "example.com." If the URL is http://example.org/ then
> the domain is "example.org."
>
> This seemed to work perfectly until I come across a URL like
> http://www.example.co.uk/
> My script thinks the domain is "co.uk."
>
> So I added a bit of code to account for this, basically if the 2nd to last
> segment of the hostname is "co" then take the last 3 segments.
>
> Then I stumbled across a URL like http://www.example.com.au/
>
> So it occurred to me that this is not the best solution, unless I have a
> definitive list of all exceptions to go off of.
>
> Does anyone have any suggestions?
>
> Any advice is much appreciated.
>
> Thanks,
> Brad
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


Maybe use some regexp like this:
$url = http://www.a-domain.co.uk;

$domain = preg_match( "!^(http://)([-a-z0-9]+(?:\.[-a-z0-9]+)*)$!i" ); //
you will get www.a-domain.co.uk // So u can search for "www." In front of
the string to replace it or // you could edit the search for ignoring the
'www.'

Echo $domain[2];


Greetings,

Stefan

Note: this is untested.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:31 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0