Extract part of an URL

Discussion in 'PHP' started by Fracisc, Jul 28, 2009.

  1. #1
    I need to extract the third part of the URL (if it is there).
    Demo URL: http://www.glitters8.com/graphics/friends/blah..etc..

    I need to extract the 3rd part (ie. friends), as long as it exist. What`s the best way to do that?
     
    Fracisc, Jul 28, 2009 IP
  2. w0tan

    w0tan Peon

    Messages:
    77
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php
    $url = 'http://www.glitters8.com/graphics/friends/blah';
    $parsed_url = parse_url($url);
    $exploded = explode('/', $parsed_url['path']);
    
    print $exploded[2];
    ?>
    
    Code (markup):
    It would be easier to get with regex, but I used parse_url since it's faster to write.
     
    w0tan, Jul 29, 2009 IP