Remove domain part of a url

Discussion in 'PHP' started by ridesign, May 1, 2008.

  1. #1
    ridesign, May 1, 2008 IP
  2. Randombase

    Randombase Peon

    Messages:
    224
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    PHP has a lovely builtin function called parse_url(), so in your case you could use:
    $urlparts = parse_url("http://www.domain.com/newsite.php?id=12");
    $extracted = $urlparts['path'].'?'.$urlparts['query'];
    print $extracted;
    PHP:
    You can also use explode() and remove the first two/three parts of the array.
     
    Randombase, May 1, 2008 IP
    ridesign likes this.
  3. ridesign

    ridesign Peon

    Messages:
    294
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Brilliant :)

    Thanks
     
    ridesign, May 1, 2008 IP
  4. ridesign

    ridesign Peon

    Messages:
    294
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How would I use the explode function to remove the domain name part as I may have additional / and & in the url
     
    ridesign, May 1, 2008 IP
  5. Randombase

    Randombase Peon

    Messages:
    224
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <?php
    $url = "http://www.lol.com/test/sweet/?value=lala&thanmorevalue=loool";
    $parts = explode("/",$url);
    array_shift($parts);array_shift($parts);array_shift($parts);
    $newurl = implode("/",$parts);
    print $newurl; //  test/sweet/?value=lala&thanmorevalue=loool
    ?>
    PHP:
    Ugly code, but since the amount of array_shift()'s is static, I'm not bothering with a for() loop :)
     
    Randombase, May 1, 2008 IP