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 6 of 6
  1. #1
    New Coder seanmarkham's Avatar
    Join Date
    Dec 2011
    Location
    Lincolnshire, England
    Posts
    50
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Exploding a string

    Hi All,
    Trying to split a string and make it possible to echo the results back onto the page, it feels close but an struggling at this hurdle.

    Any help greatly appreciated.

    PHP Code:
    <?php
    $str 
    'id=50283&date=21/02/2014&time=11:00&package=Laser Game&firstname=Rodger&lastname=Marshall&totalprice=54.50&osbalance=25.00&noofpeople=2';


    $a explode('&'$str); 

     foreach(
    $a as $key => $b
     { 
        
    $b split('='$b); 

        
    $headings htmlspecialchars(urldecode($b[0]))."<br />\n";
    $query htmlspecialchars(urldecode($b[1])).","




    list(
    $id$date$time$package$fname$lname$tprice$osbal$people) = explode(","$query);


    echo 
    $id;
    echo 
    $date;
    echo 
    $time;
    echo 
    $package;
    echo 
    $fname;
    echo 
    $lname;
    echo 
    $tprice;
    echo 
    $osbal;
    echo 
    $people;
     }
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Its far easier than this. And I'm not kidding.
    PHP Code:
    parse_str($str); 
    Literally. It is dangerous to do it directly though as it will overwrite existing variables, so I'd suggest using an array to deal with that:
    PHP Code:
    parse_str($str$arr); 
    Now $arr will have offsets for things like ['id'] within it. You can use a list or extract from that point. The offsets all match a part of the querystring key values.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder seanmarkham's Avatar
    Join Date
    Dec 2011
    Location
    Lincolnshire, England
    Posts
    50
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thank you for your help.

    I have only been learning strings for the past two hours, can you please explain how I would implement that.

    Many Thanks

    Sean

  • #4
    New Coder seanmarkham's Avatar
    Join Date
    Dec 2011
    Location
    Lincolnshire, England
    Posts
    50
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Also the string will arrive from a base64_decode, however I have implemented it like that to test results.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    base64 simply requires the decode first. I don't know where you're getting it from, so I can only stick to the use of $str.
    PHP Code:
    $str base64_decode($str);
    parse_str($str$a);
    foreach (
    $a AS $field => $value)
    {
        
    printf('%s = %s' PHP_EOL$field$value);

    This would replace what you have in its entirety (except the $str since I don't know where that comes from). You may need to use the htmlspecialchars and urldecode on the $value before using it. Probably not the url decode, but the htmlspecialchars will be required if it accepts parsable html characters.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    New Coder seanmarkham's Avatar
    Join Date
    Dec 2011
    Location
    Lincolnshire, England
    Posts
    50
    Thanks
    11
    Thanked 0 Times in 0 Posts
    The base64 parameter would be appended to the URL as follows

    http://www.yourURL.com/page.php?data...hlasdkhfdsakhf

    and I was going to use the GET function to retrieve it.

    PHP Code:
    $string $_GET['data']; 
    I have got this to work so far.

    PHP Code:
    $str = 'id=50283&date=21/02/2014&time=11:00&package=Laser Game&firstname=Russell&lastname=Preece&totalprice=54.50&osbalance=25.00&noofpeople=2';


    parse_str($str);


    ?>This is the booking ID: <? echo $id."<br />";

    ?>This is the booking date: <? echo  $date."<br />";

    ?>This is the booking time: <? echo $time."<br />";
    Thanks

    Sean


  •  

    Posting Permissions

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