Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1

    Question Regex To Grab Domain From Url


    Fellow Php Programmers,

    I found this regex to grab domain name of urls:

    /^(?:http(?:s)?:\/\/)?(?:[^\.]+\.)?example\.com$/

    Found it here:

    https://stackoverflow.com/questions/...-simple-domain

    It will grab domain name from the following type of urls:

    https://example.com
    Example Domain
    Example Domain
    https://example.com
    www.example.com
    example.com


    I also found the following code but it is not working:

    PHP Code:

    <?php

    $domain 
    parse_url($urlPHP_URL_HOST);

    echo 
    $domain;

    ?>
    Found it here:

    https://stackoverflow.com/questions/...omain-from-url

    And now found this:

    $domain = parse_url('http://' . str_replace(array('https://', 'http://'), '', $url), PHP_URL_HOST);

    Do you have any better suggestion that will not go a miss on any form of url so the domain grabbing would be a 100% guarantee with your suggested code ?
    Last edited by UniqueIdeaMan; July 1st, 2017 at 09:05 AM.
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,741
    Rep Power
    4536
    I suggest learning PHP.
    -- Cigars, whiskey and wild, wild women. --
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1
    Originally Posted by Sepodati
    I suggest learning PHP.
    Actually, I was about to go out and found this link:

    https://stackoverflow.com/questions/...omain-from-url

    I then just created a webpage on my website. Eg:

    http://mywebsite.com/grab_domain_name.php

    And pasted this code, which I grabbed from that link:

    $domain = parse_url($url, PHP_URL_HOST);

    And, I saw a blank page. Couldn't experiment with the code any more due to short time.
    Anyway, I now tried these 2 codes which I made-up myself, figuring those is how they should have been:

    PHP Code:

    <?php

    $url 
    http://google.com;
    $domain parse_url($urlPHP_URL_HOST);
    echo 
    $domain;

    ?>

    <?php

    $domain 
    parse_url(http://google.com, PHP_URL_HOST);
    echo $domain;

    ?>
    Again, blank page.
    I know I am doing something wrong and so go easy on the crticism!
    Let me see how you would code it!
    Last edited by UniqueIdeaMan; July 1st, 2017 at 09:05 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1

    Question


    Originally Posted by Sepodati
    I suggest learning PHP.
    Actually, I was about to go out and found this link:

    https://stackoverflow.com/questions/...omain-from-url

    I then just created a webpage on my website. Eg:

    http://mywebsite.com/grab_domain_name.php

    And pasted this code, which I grabbed from that link:

    $domain = parse_url($url, PHP_URL_HOST);

    And, I saw a blank page. Couldn't experiment with the code any more due to short time.
    Anyway, I now tried these 2 codes which I made-up myself, figuring those is how they should have been:

    PHP Code:

    <?php

    $url 
    http://google.com;
    $domain parse_url($urlPHP_URL_HOST);
    echo 
    $domain;

    ?>

    <?php

    $domain 
    parse_url(http://google.com, PHP_URL_HOST);
    echo $domain;

    ?>
    Again, blank page.
    I know I am doing something wrong and so go easy on the crticism!
    Let me see how you would code it!
    Last edited by UniqueIdeaMan; July 1st, 2017 at 09:29 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1
    Originally Posted by Sepodati
    I suggest learning PHP.
    Ha! Ha! I am trying to finish learning php, old buddy! Lol!

    Actually, I was about to go out and found this link:

    https://stackoverflow.com/questions/...omain-from-url

    I then just created a webpage on my website. Eg:

    http://mywebsite.com/grab_domain_name.php

    And pasted this code, which I grabbed from that link:

    $domain = parse_url($url, PHP_URL_HOST);

    And, I saw a blank page. Couldn't experiment with the code any more due to short time.
    Anyway, I now tried these 2 codes which I made-up myself, figuring those is how they should have been:

    Code:
    <?php
    
    $url = http://google.com;
    $domain = parse_url($url, PHP_URL_HOST);
    echo $domain;
    
    ?>
    Code:
    <?php
    
    $domain = parse_url(http://google.com, PHP_URL_HOST);
    echo $domain;
    
    ?>
    Again, blank page.
    I know I am doing something wrong and so go easy on the crticism!
    Let me see how you would code it!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1
    Originally Posted by Sepodati
    I suggest learning PHP.
    Ha! Ha! I am trying to finish learning php, old buddy! Lol!

    Actually, I was about to go out and found this link:

    https://stackoverflow.com/questions/...omain-from-url

    I then just created a webpage on my website. Eg:

    http://mywebsite.com/grab_domain_name.php

    And pasted this code, which I grabbed from that link:

    $domain = parse_url($url, PHP_URL_HOST);

    And, I saw a blank page. Couldn't experiment with the code any more due to short time.
    Anyway, I now tried these 2 codes which I made-up myself, figuring those is how they should have been:

    Code:
    <?php
    
    $url = http://google.com;
    $domain = parse_url($url, PHP_URL_HOST);
    echo $domain;
    
    ?>
    Code:
    <?php
    
    $domain = parse_url(http://google.com, PHP_URL_HOST);
    echo $domain;
    
    ?>
    Again, blank page.
    I know I am doing something wrong and so go easy on the crticism!
    Let me see how you would code it!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    225
    Rep Power
    1

    Thumbs up


    Fixed the issue!
    I added quotes that mention the url.
    Code that is working is this:


    Code:
    <?php
    
    /*
    ERROR HANDLING
    */
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    
    //For All Error, Warning and Notice
    error_reporting(E_ALL) OR error_reporting(-1);
    //For All Errors
    error_reporting(E_ERROR);
    //For All Warnings
    error_reporting(E_WARNING);
    //For All Notice
    error_reporting(E_NOTICE);
    
    error_reporting(E_ALL);
    
    
    
    $url = "http://google.com";
    $domain = parse_url($url, PHP_URL_HOST);
    echo $domain;
    
    
    $domain = parse_url("http://google.com/1", PHP_URL_HOST);
    echo $domain;
    
    
    $domain = parse_url("http://yahoo.com/2", PHP_URL_HOST);
    echo $domain;
    
    
    $domain = parse_url('http://ebay.com/3', PHP_URL_HOST);
    echo $domain;
    
    ?>
    Note the last one has single quote while the other 2 double.
    But, all this is related to "parse_url" built-in function. And that could fail on some urls. Therefore, still on the hunt for the perfect regex.
    Last edited by UniqueIdeaMan; June 29th, 2017 at 05:15 PM.
  14. #8
  15. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,741
    Rep Power
    4536
    You added quotes around a string? ****ing genius.

    Comments on this post

    • kicken agrees
    -- Cigars, whiskey and wild, wild women. --
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2017
    Posts
    6
    Rep Power
    0
    But, all this is related to "parse_url" built-in function. And that could fail on some urls.
    Why is it likely to fail? It will only fail if you don't feed it a valid URL.
  18. #10
  19. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,741
    Rep Power
    4536
    Originally Posted by UniqueIdeaMan
    But, all this is related to "parse_url" built-in function. And that could fail on some urls.
    Such as what?
    -- Cigars, whiskey and wild, wild women. --
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17
    Originally Posted by Greum
    Why is it likely to fail? It will only fail if you don't feed it a valid URL.
    It will not fail at what it is meant to do, which is not to validate URLs.
    This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly.
    Read this very concise quote carefully. It's not a validation tool, it simply parses a URL, and tries to parse partial / bad ones, too. Using it for validation is stupid. Use a regex or the FILTER_VALIDATE_URL predefined constant, unless of course you are an idiot.

    Comments on this post

    • UniqueIdeaMan agrees
    Last edited by Arty Zifferelli; July 1st, 2017 at 02:44 PM.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2017
    Posts
    6
    Rep Power
    0
    which is not to validate URLs
    Who said ANYTHING about using it to validate a URL???
  24. #13
  25. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17
    Originally Posted by Greum
    Who said ANYTHING about using it to validate a URL???
    Let's see...
    Originally Posted by Greum
    Why is it likely to fail? It will only fail if you don't feed it a valid URL.
    "Valid" as in "validation". You know? Validation as in to make valid. The English language, it's a thing!

    It does not fail if you feed it an invalid URL because it is not a validation function. Do you know the difference between parsing and validating? Possibly not.

    In fact it does not fail at all. It parses valid URLs, partial URLs, and tries to parse "bad" URLs. But it does not "fail" in any way.

    Hmmm... "Greum"... From an English place name meaning "Granta's estate." Scottish, are you?
    Last edited by Arty Zifferelli; July 1st, 2017 at 02:44 AM.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2017
    Posts
    6
    Rep Power
    0
    Yes I did say, "It will only fail if you don't feed it a valid URL" in response to the OP saying it could fail on some URLs, but that doesn't suggest using the function to validate a URL does it? Or is my English really that crap...
  28. #15
  29. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17
    Originally Posted by Greum
    Yes I did say, "It will only fail if you don't feed it a valid URL" in response to the OP saying it could fail on some URLs, but that doesn't suggest using the function to validate a URL does it? Or is my English really that crap...
    Perhaps. Perhaps not. Hey! Look over there!
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo