Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts

    Returning URI segments

    Hi Guys,

    I was curious if i have a url like this.

    www.example.com/dir/bob/photos

    is there a way i can isolate the parts of the url by calling them specifically.

    like this.
    uri($path, 3) = photos
    uri($path, 1) = dir

    Thanks in advance.

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    yes many ways one such way is to explode them into an array:

    PHP Code:
    <?php
    $path
    ="http://www.example.com/dir/bob/photos";
    $segments=explode("/"$path);

    //display first segment
    echo $segments[0]; // Note first allways start with index 0

    //display second
    echo $segments[1];

    //display last
    echo $segments[count[$segment]-1]; // Note because indexes start at 0 have to deduct one from count function

    // loop through all segments
    foreach($segments $key => $value)

         echo 
    "segment"$key "=" $value ."<br />";
    }
    ?>
    There are other things you can do with regex to remove the domain part of the url.

    In my example though the first would be http:
    the second would be NULL
    The third would be www.example.com
    and so on
    The last would be photos.

    In your example www.example.com/dir/bob/photos
    the first would be www.example.com
    the second dir
    etc
    Last edited by timgolding; 04-30-2009 at 12:40 AM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    If you want to get the full url path to the page you are in you can use
    PHP Code:
    $path=$_SERVER['PHP_SELF']; 
    There are other example of similar things you can get about the script you are in here http://uk.php.net/manual/en/reserved...les.server.php
    You can not say you know how to do something, until you can teach it to someone else.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Here's a stripped down snippet from a CMS I have written:
    PHP Code:
    /*
     * segments ( [ int $index = NULL ] ) -> string
     */

    function segments$index NULL )
    {
        static 
    $segments;
        
    // build $segments on first function call
        
    if ( NULL === $segments )
        {
            
    $segments parse_url$_SERVER['REQUEST_URI'], PHP_URL_PATH );
            
    $segments explode'/'$segments );
            
    $segments array_filter$segments );
            
    $segments array_values$segments );
        }
        
    // if $index is NULL, emulate REQUEST_URI
        
    if ( NULL === $index )
        {
            return 
    '/' implode'/'$segments );
        }
        
    // return the segment index if valid, otherwise NULL
        
    $index =  ( int ) $index 1;
        return isset( 
    $segments[$index] ) ? $segments[$index] : NULL;

    PHP Code:
    // URI: /this/is//a/path/to///nothing
    echo segments);  // path
    echo segments18 ); // NULL
    echo segments();     // /this/is/a/path/to/nothing 


  •  

    Posting Permissions

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