Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    I can not seem to extract this domain from this url

    Hello,

    I am trying to use parse_url() to extract the domain from
    the url.

    This is the function I am using:

    PHP Code:
    function GetDomain($url) {
      
    $nowww trim(ereg_replace('www\.','',$url));
      
    $domain parse_url($nowww);
      if(!empty(
    $domain["host"])) {
         return 
    $domain["host"];
         } 
        else  {
         return 
    "No DOM"//  $domain["path"];
         
    }
     }

    $m_url trim($sal_page_lin);
    $m_url  ereg_replace('http:\/\/','',$m_url);
    $m_dom GetDomain($m_url);    

    write_log("$cnt ) INSERTED to c2s_urls: Id: $prod->id, URL: $m_url DOM: $m_dom\r\n"); 
    The output from my log gives:


    1 ) INSERTED to c2s_urls: Id: 8988, URL: www.700inaday.com DOM: No DOM
    2 ) INSERTED to c2s_urls: Id: 9223, URL: www.selfemployedcourier.com/forAffiliates.html DOM: No DOM
    3 ) INSERTED to c2s_urls: Id: 9698, URL: www.howtomarketexperts.com DOM: No DOM

    So for some reason my GetDomain() function isn't working.

    Can anyone see what I have done wrong ?


    Thanks.



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    what are you trying to get? just .com ?
    using host will only return www. but your already removing that.

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Yer,

    I an trying to get the root domain.

    i.e. in the examples given I would expect this output:

    1 ) INSERTED to c2s_urls: Id: 8988, URL: www.700inaday.com DOM: 700inaday.com
    2 ) INSERTED to c2s_urls: Id: 9223, URL: www.selfemployedcourier.com/forAffiliates.html DOM: selfemployedcourier.com
    3 ) INSERTED to c2s_urls: Id: 9698, URL: www.howtomarketexperts.com DOM: howtomarketexperts.com


    And from something like this:

    justin.howard.stars.net/profiles/march23.php?id=ght466&help=yes

    I would expect:
    stars.net

    So is there an attribute I can use to get the root domain ?


    Thanks.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try this;

    PHP Code:
    function GetDomain($url) { 
      
    $nowww trim(ereg_replace('www.','',$url)); 
      
    $domain parse_url($nowww); 
      if(!empty(
    $domain["path"])) { 
         return 
    $domain["path"]; 
         }  
        else  { 
         return 
    "No DOM"//  $domain["path"]; 
         

     } 

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    PHP Code:
      $nowww trim(ereg_replace('www.','',$url)); 
    Ereg functions are deprecated. A simple str_replace would work.

    Code:
    $nowww = trim(str_replace('www.', '', $url));

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Hmm, just looking at this one on the API:
    Note: This function doesn't work with relative URLs.
    If you do a var_dump on the $domain inside of the getDomain, my assumption is it will come up false? If so, correct this by removing the following line:
    PHP Code:
    $m_url  ereg_replace('http:\/\/','',$m_url); 
    And changing the function as follows (though I suspect this will actually work still without the alterations since www doesn't factor into creating and absolute URL)
    PHP Code:
    function GetDomain($url) { 
      
    $domain parse_url($nowww); 
      if(!empty(
    $domain["path"])) { 
         return 
    trim(str_replace('www.'''$domain["path"])); 
         }  
        else  { 
         return 
    "No DOM"//  $domain["path"]; 
         

     } 
    Post back results.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Ran function as:

    PHP Code:
    function GetDomain($N_url) { 
      
    $domain parse_url($N_url); 
      if(!empty(
    $domain["path"])) { 
         return 
    trim(str_replace('www.'''$domain["path"])); 
         }  
        else  { 
         return 
    "No DOM"//  $domain["path"]; 
         

     }  


    $N_url trim($sal_page_lin); 
    $m_dom GetDomain($N_url);    
    $m_url  ereg_replace('http:\/\/','',$N_url); 

    Results:

    1 ) Id: 8988, URL: www.700inaday.com DOM: No DOM
    2 ) Id: 9223, URL: www.selfemployedcourier.com/forAffiliates.html DOM: /forAffiliates.html
    3 ) Id: 9698, URL: www.howtomarketexperts.com DOM: No DOM
    4 ) Id: 9704, URL: www.4inonesystem.com/welcome.html DOM: /welcome.html
    5 ) Id: 556, URL: www.lcd-monitor-repair.com DOM: lcd-monitor-repair.com

    As we can see it is not working

    PS .

    I changed the position of this line:
    $m_url = ereg_replace('http:\/\/','',$N_url);
    because I want to to store the url without the http: as I add it
    back in after I read it from the database in another script.

    Maybe I should change that to :
    $m_url = trim(str_replace('http://'', '', $url));
    would that work without escaping the "/" ?

    Thanks for advice.



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    that function that I posted worked, I've tested it.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Hah, sorry you're using the wrong field. Path is for anything after the / and before the ?. So, what you want is 'host', not path. Try making those changes to see if thats what you want.

    Edit:
    I'm a little concerned about those results for number 5 though, something doesn't jive there....
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #10
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi,

    Thanks for the input.

    This is the function I used:
    PHP Code:
    function GetDomain($N_url) { 
      
    $domain parse_url($N_url); 
      if(!empty(
    $domain["host"])) { 
         return 
    trim(str_replace('www.'''$domain["host"])); 
         }  
        else  { 
         return 
    "No DOM"//  $domain["path"]; 
         

     }  

    $N_url trim($sal_page_lin); 
    $m_dom GetDomain($N_url);    

    write_log("$cnt ) Id: $prod->id, URL: $N_url DOM: $m_dom\r\n"); 
    More of them are getting a DOM but some are not
    and I can not see why.

    Results:

    1 ) Id: 8988, URL: http://www.700inaday.com DOM: 700inaday.com
    2 ) Id: 9223, URL: http://www.selfemployedcourier.com/forAffiliates.html DOM: selfemployedcourier.com
    3 ) Id: 9698, URL: http://www.howtomarketexperts.com DOM: howtomarketexperts.com
    4 ) 9704, URL: http://www.4inonesystem.com/welcome.html DOM: 4inonesystem.com
    5 ) Id: 556, URL: www.lcd-monitor-repair.com DOM: No DOM
    11 ) Id: 3504, URL: www.timelesshealth.net/kefir/grains.html DOM: No DOM
    12 ) Id: 441, URL: http://www.crazy-tattoo-designs.com/...on_pay_now.htm DOM: No DOM
    18 ) Id: 2647, URL: www.timelesshealth.net/ebook DOM: No DOM

    Ah har.

    So it seems that some of my usrls do not have the "http://" and these
    are the ones that are failing.

    So I guess I need to add an if statement ?

    if( substr($N_url,0,7) != 'http://' ) $N_url = 'http://'.$N_url;

    Should that do the trick ?


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Interestingness. Seems like a bug if you ask me, but I found this in the user notes:
    nirazuelos at gmail dot com
    09-Oct-2009 09:45
    Hello, for some odd reason, parse_url returns the host (ex. example.com) as the path when no scheme is provided in the input url. So I've written a quick function to get the real host:
    PHP Code:
    <?php 
    function getHost($Address) { 
       
    $parseUrl parse_url(trim($Address)); 
       return 
    trim($parseUrl[host] ? $parseUrl[host] : array_shift(explode('/'$parseUrl[path], 2))); 


    getHost("example.com"); // Gives example.com 
    getHost("http://example.com"); // Gives example.com 
    getHost("www.example.com"); // Gives [url]www.example.com[/url] 
    getHost("http://example.com/xyz"); // Gives example.com 
    ?>
    You could try anything! It gives the host (including the subdomain if exists).

    Hope it helped you.
    Seems like that matches you're issue!

    Edit:
    Also, you'll want to add the apostrophies within the index access. Can't believe that someone made a user-note that wasn't checked for errors O.o
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #12
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Also, you'll want to add the apostrophies within the index access.
    Not quite sure what you mean by that ???


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by jeddi View Post
    Not quite sure what you mean by that ???


    .
    $parseUrl[host] should be $parseUrl['host'] or $parseUrl["host"]. Otherwise, it will trigger a notice when attempting to access the constant host prior to casting it to the string 'host'. Doesn't stop processing, but will be problematic should 'host' ever become a defined constant.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •