regex for getting domain from url or other methods?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fris
    Too lazy to set a custom title
    • Aug 2002
    • 55697

    #1

    regex for getting domain from url or other methods?

    how can I test how fast a regex is compared to others?

    I want to be able to get the domain from any url.

    Code:
    <?php
    
    function get_hostname($url) {
      if (preg_match('#([0-9a-z.-]+)#i',preg_replace('#^(?:(?:\w+):(?:\\?)?)#i','',$url),$matches)) return $matches[1];  
    }
    
    $hostname = 'http://www2.example.com:80/test/evil--kj12/cb_2/1.html';
    echo $hostname . '<br/> '. get_hostname($hostname);
     
    ?>
    this is what i got so far, works just curious any others got any other ways
    Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.
  • VideoJ
    Confirmed User
    • Aug 2002
    • 750

    #2
    http://php.net/manual/en/function.parse-url.php
    Somebody stole my damn signture...

    Comment

    Working...