SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code to extract ONLY the domain.com part of an address?

    I am trying to find some code that will "extract" only the domain.com portion of some text input.

    For example of possible input:

    www.domain.com
    http://domain.com
    http://www.domain.com

    Basically, I need only the "www." and "http://" part striped away.. if it exists.

    Thanks!

  2. #2
    SitePoint Zealot Hulkur's Avatar
    Join Date
    Oct 2001
    Location
    Estonia
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preg_replace('/((^http:\/\/www\.)|(^http:\/\/)|(^www\.))/', '', $input);

    you can't use just (www\.) as it will cut out all 'www.' from string: www.thiswww.com -> this.com
    (2B) or (not 2B) = FF

  3. #3
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm not a regex expert so i dont know the exact regex to use but in Hulkur example it would be better so that you cover all possible cases to not use http: or www in the regex but to do (in english not regex) search from beginning of string for any characters EXCEPT a period until you get to the first period then stop so you would cover http://blah.domain.com as well. Sorry that i dont have the regex for ya. Maybe a guru will be able to post it for us?
    Erh

  4. #4
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe this '^[^.]*\.' not sure if the * is in the right place.
    Erh

  5. #5
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to have a look at parse_url.

  6. #6
    SitePoint Zealot Hulkur's Avatar
    Join Date
    Oct 2001
    Location
    Estonia
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you want domain starting from first dot then:

    preg_replace('/([^\.]+\.)/', '', $input);
    (2B) or (not 2B) = FF


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
  •