-2

Possible Duplicate:
how to get domain name from URL
PHP Getting Domain Name From Subdomain

As i figured out a lot from the examples from the google there are not so universal for getting the domain name from url because of:

  1. www
  2. subdomains
  3. second-level domain (co.uk)

Is there any solution, that can get universally domain.

Community
  • 1
  • 1
NoNameZ
  • 785
  • 4
  • 14
  • 22
  • 3
    Look to the right. See the "Related" section? -----> – Paul Dessert Jun 20 '12 at 00:08
  • 1
    http://stackoverflow.com/questions/1201194/php-getting-domain-name-from-subdomain – Adam Jun 20 '12 at 00:10
  • @Paul yes, as see, but as i said before, there is no solution with those 3 things included in one function. – NoNameZ Jun 20 '12 at 00:12
  • @Adam there is line: "EDIT: Ignore the .co.uk, presume that all domains going through this function have a 3 letter TLD." – NoNameZ Jun 20 '12 at 00:13
  • Why 3 letter TLD? What about country domains (.ca, .cn etc). Do you mean to exclude those? – jpiasetz Jun 20 '12 at 00:25
  • this one is not easy (with regular expressions only), because "co" in "co.uk" is (per definition) actually the domain name and everything before ("bbc" in "bbc.co.uk") is already a subdomain. Compare "www.myhost.3.ly" to "www.bbc.co.uk" and you undertand what i mean. [see this post with a few examples on short domains](http://singlefunction.com/the-shortest-url-shorteners/) – Kaii Jun 20 '12 at 00:35

1 Answers1

0

I use the following regex in Ruby and it works for me:

/(https?:\/\/([-\w\.]+)+(:\d+)?(\/([-\w\/_\.]*(\?\S+)?)?)?)/

Example: http://rubular.com/r/wyR7yvKT6q

But what you mean under domain? The above regexp extracts domain correctly

http://my.subdomain.ko.uk
to
my.subdomain.ko.uk <-- this is domain

Or you can use:

.*([^\.]+)(com|net|org|info|coop|int|co\.uk|org\.uk|ac\.uk|uk|__and so on__)$
/^(?:www\.)?(.*?)\.(?:com|au\.uk|co\.in)$/
odiszapc
  • 4,089
  • 2
  • 27
  • 42