SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    nice urls, REQUEST_URI and array

    Hi,

    I have nice urls: /foor/var/id/2/

    Please consider the following code:

    PHP Code:
    $requestUri explode("/",$_SERVER['REQUEST_URI']); 
    I have tried many ways to associate correctly my nice url in a key => value fashion, without success. I'm sure it must be fairly simple to someone with mroe experience.


  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    which ones are the keys, and which ones are the values?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,904
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    $requestUri explode("/",$_SERVER['REQUEST_URI']);  
    for (
    $i 0$i count($requestUri); $i += 2) {
        ${
    $requestUri[$i]} = isset($requestUri[$i+1]) ? $requestUri[$i+1] : null;
    }
    var_dump($GLOBALS); 
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Start at 1, Remon. 0 will be the blank string before the first /.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,904
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Right, or we could remove all slashes at the start of the string, which I think is a bit nicer because it also avoids accidental double slashes problems.

    PHP Code:
    $requestUri ltrim('/'explode("/",$_SERVER['REQUEST_URI']));
    for (
    $i 0$i count($requestUri); $i += 2) {
        ${
    $requestUri[$i]} = isset($requestUri[$i+1]) ? $requestUri[$i+1] : null;
    }
    var_dump($GLOBALS); 
    For completeness sake we could check if count($requestUri) %2 == 0 to see that each key has a value, but I'm not sure what to do if this is not the case, so I'll leave that to the OP.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •