SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member auveeb's Avatar
    Join Date
    Oct 2005
    Location
    NY, USA
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Strip everything but the domain name?

    Hey,

    I have a string that reads "http://www.domain.com/index/a/b". How would I go about stripping that so that it's only "domain.com"?

    I've tried a ton of stuff but can't come up with anything. Could anyone chime in?

    Regards

  2. #2
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    should work for any legitimate url:
    PHP Code:
      $url 'http://www.domain.com/index/a/b';
       
    preg_match("#^(?:http://)?(?:www\.)?([\w\.\-]+)#"$url$matches);
         
    $domain =  $matches[1]; 

  3. #3
    SitePoint Addict GeertDD's Avatar
    Join Date
    Feb 2005
    Location
    Belgium
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $url 'http://www.domain.com/index/a/b';

    $tld parse_url($url);
    $tld preg_replace('/^www\./'''$tld['host']);

    echo 
    $tld
    Edit:

    aamonkey was faster. I like his code better too, does the job with one function.

  4. #4
    SitePoint Member auveeb's Avatar
    Join Date
    Oct 2005
    Location
    NY, USA
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dang, thanks. I really need to start learning regex.

  5. #5
    SitePoint Addict GeertDD's Avatar
    Join Date
    Feb 2005
    Location
    Belgium
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I'm wondering about what the characters in blue are for? Could you explain those? Thanks.

    ^(?:http://)?(?:www\.)?([\w\.\-]+)

  6. #6
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the ?: at the start of a subpattern makes that sub-pattern non-capturing. for example, if I have a string '123abc' and i use the pattern (\d+)(\w+) with preg_match, $matches[1] will be 123 and $matches[2] will be abc.
    But if I change the pattern to (?:\d+)(\w+) now $matches[1] will be abc because I've told it to ignore the 1st subpattern.

    I generally use it when I can as it makes the pattern run a little faster and (i think) saves memory....

  7. #7
    SitePoint Addict GeertDD's Avatar
    Join Date
    Feb 2005
    Location
    Belgium
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aha, great. I've learnt something new. Thank you!


Bookmarks

Posting Permissions

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