2

I need to get domain name from URL excluding "www" and ".com" or ".co.uk" or anything other.

Example-

I have following urls like-

http://www.example.com
http://www.example.co.uk
http://subdomain.example.com
http://subdomain.example.co.uk

There will be anything at ".com" , ".org" , ".co.in", ".co.uk".

Satish Shinde
  • 2,878
  • 1
  • 24
  • 41
  • possible duplicate of [Remove domain extension](http://stackoverflow.com/q/3853338/53114) – Gumbo Nov 07 '14 at 07:54

3 Answers3

3

I try this it work for me.

    $original_url="http://subdomain.example.co.uk"; //try with all urls above

    $pieces = parse_url($original_url);
    $domain = isset($pieces['host']) ? $pieces['host'] : '';
    if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain,    $regs)) {
       echo strstr( $regs['domain'], '.', true );
    }

Output- example

I get this from Here Get domain name from full URL

Community
  • 1
  • 1
Satish Shinde
  • 2,878
  • 1
  • 24
  • 41
2
(?:https?:\/\/)?(?:www\.)?(.*)\.(?=[\w.]{3,4})

Try this.See demo.Grab the capture.

http://regex101.com/r/bW3aR1/2

vks
  • 67,027
  • 10
  • 91
  • 124
1

You should use the PHP function parse_url() in combination with a str_replace() or regex, or maybe even an explode. It depends on a few things:

Things to note:

  • Will there always be a subdomain?
  • Will there be a specific list of allowed subdomains?

I would do something like this:

<?php
$url = 'http://www.something.com';
$parts = explode('.', parse_url($url, PHP_URL_HOST));
echo $parts[1]; // "something"
LeonardChallis
  • 7,759
  • 6
  • 45
  • 76