#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    318
    Rep Power
    1

    Question Regex To Grab Domain


    Folks,

    I need a regex to extract the domain name from any url. No matter what the url looks like. Whether it has more than one dot (2nd level, 3rd level, 4th level, 5th level, etc. level domain) and whether or not it has the http://, https://, www., etc.).
    In short, it must work on any url in the world. No matter what format the url is in aslong as the url is a valid one. No matter how good, ugly, short or long the url is.
    I am spoiled for choice as too many samples are provided here and I don't know which one will best suit my purpose:
    https://stackoverflow.com/questions/...-name-from-url

    Which one is your best pick for my purpose and why that one over the others ?
    Last edited by UniqueIdeaMan; October 17th, 2017 at 05:37 PM.
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,327
    Rep Power
    2063
    Regex? *shudders*

    No need.

    Again... The PHP manual is your friend. You really need to learn how to use it.

    PHP: parse_url - Manual
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    318
    Rep Power
    1
    Originally Posted by Catacaustic
    Regex? *shudders*

    No need.

    Again... The PHP manual is your friend. You really need to learn how to use it.

    PHP: parse_url - Manual

    Would you use the parse_url over all the regex codes mentioned here:
    https://stackoverflow.com/questions/...-name-from-url
    If parse_url is fine then why did those programmers in that above mentioned link bother with regex ?

    Is my parse_url code ok ?

    PHP Code:
        <?php

        
    include 'config.php';

        
    $url "http://one.com/1.php";
        
    $domain parse_url($urlPHP_URL_HOST);
        echo 
    $domain;

        
    $url "http://two.com/2.htm";
        
    $domain parse_url($urlPHP_URL_HOST);
        echo 
    $domain;


        
    $domain parse_url("http://three.com/3.html"PHP_URL_HOST);
        echo 
    $domain;


        
    $domain parse_url("http://four.com/4.shtm"PHP_URL_HOST);
        echo 
    $domain;


        
    $domain parse_url('http://five.com/5.shtml'PHP_URL_HOST);
        echo 
    $domain;

        
    $domain parse_url('http://usr:pss@example.com:81/mypath/myfile.html?
        a=b&b[]=2&b[]=3#myfragment'
    PHP_URL_HOST);
        echo 
    $domain;

        
    ?>
    Notice how many types of urls I tested with parse_url. Parse_url seems to manage to grab the 2nd level domain as I want.
  6. #4
  7. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,327
    Rep Power
    2063
    Originally Posted by UniqueIdeaMan
    Would you use the parse_url over all the regex codes mentioned here:
    Yes. The majority of those regex codes are terrible, and would need modifications to make them work in the "real world". parse_url() just works on everything.

    Originally Posted by UniqueIdeaMan
    If parse_url is fine then why did those programmers in that above mentioned link bother with regex ?
    You'd have to ask them, but most likely they didn't know about it, or they just like to use tools they know even if they aren't the best tool for that job.

    Originally Posted by UniqueIdeaMan
    Notice how many types of urls I tested with parse_url. Parse_url seems to manage to grab the 2nd level domain as I want.
    Good, so it's the right tool for the job.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    218
    Rep Power
    17
    I'm sorry, but have you not heard of this new thing called Google? Serious question: Ever used it?

    The reason I asked is that while I don't know for sure because it wouldn't be my method of choice, I do indeed suspect that it can be done with regex - since everything can be done with regex - and the first thing I would do would be to "Google" it.

    To "Google" is a verb. Basically, you navigate to https://www.Google.com, enter your question into the little box, and than clicky the "Google Search" button.

    Here's a picture => google.png No, you awful people, it is not Goatse...

    Amazing things happen! As if by magick, Google presents you with a list of possible answers!

    MIND BLOWING!

    I have to tell you, this Google thing has changed my life because now I can be as dumb as a brick, and it doesn't matter! Google knows all!

    Having said that, since there are php functions that do this domain name thingy, regex would not be my first choice...

    And you know, come to think about it, there's another resource I seem to use a lot, it's this thingy right HERE, I'm guessing from having read a lot of your questions, that you don't know about it.

    Cheers!
    Last edited by Arty Zifferelli; November 12th, 2017 at 10:45 PM.

IMN logo majestic logo threadwatch logo seochat tools logo