Yes anything can be there in the URL & I want to extract the domain name from it.

    12 days later
    stevemcgee;10998306 wrote:

    Yes, it is possible in PHP:

    $domain = "$_SERVER['HTTP_HOST']";
    echo $domain;
    In this code you get www url.

    $domain = str_replace("www.","", $_SERVER['HTTP_HOST']);
    echo $domain;
    In this code you get with out www url.

    That method fails the third test case the OP supplied.

      I came up with this, however it fails with multiple TLDs like in your 4th example in the OP:

      $url = 'http://www.example.com/page.php?q=something#anchor';
      preg_match('@^(https?://)?([a-z0-9_-]+\.)*([a-z0-9_-]+)\.[a-z]{2,6}(/.*)?$@i',$url,$match);
      $domain = $match[3];
        Write a Reply...