SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How do I explode this string and get the user id

    Hello,

    I have this string:
    Code:
    $String = "profile.php?userId=55";
    I want to get the number 55, this code just not do it:

    Code:
    $getID = explode("=",$String,2);
    
    echo $getID[2],"<br/>";
    Please help
    Thanks

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    http://php.net/manual/en/reserved.variables.get.php

    Oops, I didn't read your question too well. My answer makes no sense, sorry.
    Last edited by guido2004; Dec 6, 2012 at 11:04. Reason: stupid answer

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,060
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    It's in $getID[1];, not $getID[2];. In PHP array's are 0-based, i.e., the first part is in $getID[0]; and the second part is in $getID[1];.

    In cases like this it's always a good idea to do a var_dump($getID); to see what's going on.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    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,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Also... i wouldnt do it this way unless you -know- that the URL will only ever contain one variable.

    For example; if someone sent this as your string:

    "profile.php?userId=55&moo=cow"

    Then your script will tell you that the value of userId is "55&moo=cow"
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ketting00 View Post
    Hello,

    I have this string:
    Code:
    $String = "profile.php?userId=55";
    I want to get the number 55, this code just not do it:

    Code:
    $getID = explode("=",$String,2);
    
    echo $getID[2],"<br/>";
    For this purpose I'd suggest using some more elegant solutions:

    1. parse_url() to get the query part.
    2. parse_str() to get the userId. This way your code is future-proof in case you add more parameters in the future.

  6. #6
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thank you Lemon,

    That's exactly what I'm looking for. Very appreciated.


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
  •