SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is the best way to strip the http:// from URL's?

    How would I strip the http:// from URL's?

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    You can use str_replace to remove the beginning http://

    PHP Code:
    echo str_replace('http://''''http://www.domain.com'); 

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2004
    Location
    Sofia, Bulgaria
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and also keep in mind that this function is case-sensitive.. case-insensitive version of this function is available in PHP 5 CVS only..

  4. #4
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Chris82
    PHP Code:
    echo str_replace('http://''''http://www.domain.com'); 
    Will that give me any sort of an error if a domiain doesn't have http:// in it?

  5. #5
    SitePoint Evangelist
    Join Date
    Feb 2004
    Location
    Sofia, Bulgaria
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RockNRollPig
    Will that give me any sort of an error if a domiain doesn't have http:// in it?
    no.. if you want to know if there was no replacement:
    PHP Code:
    $domain 'www.bla.com';
    $domain2 str_replace('http://'''$domain);
    if (
    $domain != $domain2) {
      
    $domain $domain2;
    } else {
      echo 
    "error if a domiain doesn't have http:// in it";

    or better:
    PHP Code:
    if (strpos($domain'http://')) { 
      
    $domain str_replace('http://'''$domain);
    } else {
      echo 
    "error if a domiain doesn't have http:// in it";


  6. #6
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, it won't give you an error.

    Using dacoon's suggestion this is case insensitive:

    PHP Code:
    echo preg_replace('#http://#i''''http://www.domain.com'); 


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
  •