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
    New Coder
    Join Date
    Dec 2006
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How do I convert a string to multiple variables?

    Very simple one guys (I think!).

    Let's say I retrieve a $POST from the URL, and the entire value string of the new variable looks like this:

    abc_1:def_3:xyz_7

    How do I convert this string into three different variables, namely:

    abc = 1
    def = 3
    xyz = 7

    It would be easier to have those as separate variables within the URL, I know, but in this instance, I can't - they must be part of the same URL variable, and then I need to split it up into three using PHP.

    Thanks!

  • #2
    Regular Coder
    Join Date
    Apr 2009
    Location
    somewhere over the rainbow
    Posts
    105
    Thanks
    2
    Thanked 14 Times in 14 Posts
    :. -= | Eran | =- .:
    __________________

  • #3
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    PHP Code:
    $results = Array();

    $string $_POST['whatever'];
    $temp explode(":"$string); // Split at the : to get an array of "abc_1", "def_3", "xyz_7"
    for($i 0$i count($temp); $i++) {
        
    $temp2 explode("_"$temp[$i]); // Split at the underscores to get an array like "abc", "1"
        
    $results[$temp2[0]] = $temp2[1];

    That should make

    Code:
    $results['abc'] = 1;
    $results['def'] = 2;
    $results['xyz'] = 3;

  • Users who have thanked bacterozoid for this post:

    DeaDLocK (09-12-2009)

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you sir!


  •  

    Posting Permissions

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