<?php
void parse_str ( str $encoded_string [, arr &$result ]
where,
$encoded_string = The input STRING
&$result = Where the variables is stored
( PASSED by REFERENCE )
?>
<?php
$encoded_str01 = 'var01=10&var02=11';
parse_str ($encoded_str01, $arr_result01);
print_r($arr_result01);
echo '<br><br>';
var_dump($arr_result01);
echo '<br><br>';
foreach($arr_result01 as $arr_res01 => $arr_r01)
{
echo $arr_res01 . ' = ' . $arr_r01 . '<br>';
}
echo '<br>';
echo 'var01 = ' . $arr_result01['var01'] .
'<br>var02 = ' . $arr_result01['var02'] .
'<br><br>';
?>
<?php
$strtok02 = 'name="August", age=95';
echo $strtok02 . '<br><br>';
$tks02 = ',';
$strtok02 = strtok($strtok02, $tks02) .
'<br>' . strtok($tks02);
echo $strtok02 . '<br><br>';
$needl02 = ['<br>'];
$rpl02 = ['&'];
$strrep02 = str_ireplace($needl02,
$rpl02,
$strtok02 ,
$cnt02);
echo $strrep02 . '<br><br>';
parse_str ($strrep02, $arr_result02);
print_r($arr_result02);
?>
<?php
echo "Testing parse_str() : basic functionality.<br>";
echo "<br>Basic test WITH undefined var for result arg:<br>";
$s1 = "first=val1&second=val2&third=val3";
echo "<br>= = = = = = = = =<br>Given:<br>\"$s1\"<br><br>";
var_dump(parse_str($s1, $res1));
echo '<br>- - - <br><br>';
var_dump($res1);
echo "<br><br>Basic test WITH existing
non-array var for result arg:<br>";
$res2 =99;
echo "<br>= = = = = = = = =<br>Given:<br>$res2<br><br>";
$s1 = "first=val1&second=val2&third=val3";
var_dump(parse_str($s1, $res2));
echo '<br>- - - <br><br>';
var_dump($res2);
echo "<br><br>Basic test with an existing
array as results array<br>";
$res3_array = [1,2,3,4];
echo "<br>= = = = = = = = =<br>Given:<br>";
var_dump($res3_array);
echo "<br><br>";
var_dump(parse_str($s1, $res3_array));
echo '<br>- - - <br><br>';
var_dump($res3_array);
echo "<br>= = = = = = = = =";
?>
<?php
echo "Testing parse_str() :
non-default arg_separator.input specified.<br>";
$s1 = "first=val1/second=val2/third=val3";
echo "<br>Given \$s1 = <br>\"$s1\"<br><br>";
echo "After:<br>parse_str(\"$s1\", \$result)<br><br>";
var_dump(parse_str($s1, $result));
echo "<br><br>\$result = <br>";
var_dump($result);
?>
<?php
echo "Testing parse_str() : basic functionality.<br>";
echo "<br>Test string with array values and results array:<br>";
$s1 = "first=abc&a[]=123&a[]=false&b[]=str&c[]=3.5&a[]=last";
echo "<br>Given \$s1 = <br>$s1<br><br>";
var_dump(parse_str($s1, $res3_array));
echo '<br><br>';
var_dump($res3_array);
echo "<br><br><br>Test string containing numerical array keys:<br>";
$str = "arr[1]=sid&arr[4]=bill";
echo "<br>Given \$str = <br>$str<br><br>";
var_dump(parse_str($str, $res));
echo '<br><br>';
var_dump($res);
echo "<br><br><br>Test string containing associative keys:<br>";
$str = "arr[first]=sid&arr[forth]=bill";
echo "<br>Given \$str = <br>$str<br><br>";
var_dump(parse_str($str, $res));
echo '<br><br>';
var_dump($res);
echo "<br><<br>br>Test string with encoded data:<br>";
$s1 = "a=%3c%3d%3d%20%20foo+bar++%3d%3d%3e&b=%23%23%23Hello+World%23%23%23";
echo "<br>Given \$s1 = <br>$s1<br><br>";
parse_str($s1, $res);
echo '<br><br>';
var_dump($res);
echo "<br><br><br>Test string with single quotes characters:<br>";
$s1 = "firstname=Bill&surname=O%27Reilly";
echo "<br>Given \$s1 = <br>$s1<br><br>";
var_dump(parse_str($s1, $res));
echo '<br><br>';
var_dump($res);
echo "<br><br><br>Test string with backslash characters:<br>";
$s1 = "sum=10%5c2%3d5";
echo "<br>Given \$s1 = <br>$s1<br><br>";
var_dump(parse_str($s1, $res));
echo '<br><br>';
var_dump($res);
echo "<br><br><br>Test string with double quotes data:<br>";
$s1 = "str=A+string+with+%22quoted%22+strings";
echo "<br>Given \$s1 = <br>$s1<br><br>";
var_dump(parse_str($s1, $res));
echo '<br><br>';
var_dump($res);
echo "<br><br><br>Test string with nulls:<br>";
$s1 = "str=A%20string%20with%20containing%20%00%00%00%20nulls";
echo "<br>Given \$s1 = <br>$s1<br><br>";
var_dump(parse_str($s1, $res));
echo '<br><br>';
var_dump($res);
echo "<br><br><br>Test string with 2-dim array with numeric keys:<br>";
$str = "arr[3][4]=sid&arr[3][6]=fred";
echo "<br>Given \$str = <br>$str<br><br>";
var_dump(parse_str($str, $res));
echo '<br><br>';
var_dump($res);
echo "<br><br><br>Test string with 2-dim array with null keys:<br>";
$str = "arr[][]=sid&arr[][]=fred";
echo "<br>Given \$str = <br>$str<br><br>";
var_dump(parse_str($str, $res));
echo '<br><br>';
var_dump($res);
echo "<br><br><br>Test string with 2-dim array with non-numeric keys:<br>";
$str = "arr[one][four]=sid&arr[three][six]=fred";
echo "<br>Given \$str = <br>$str<br><br>";
var_dump(parse_str($str, $res));
echo '<br><br>';
var_dump($res);
echo "<br><br><br>Test string with 3-dim array with numeric keys:<br>";
$str = "arr[1][2][3]=sid&arr[1][2][6]=fred";
echo "<br>Given \$str = <br>$str<br><br>";
var_dump(parse_str($str, $res));
echo '<br><br>';
var_dump($res);
?>
<?php
echo "Test string with badly formed strings.";
$str = "arr[1=sid&arr[4][2=fred";
echo "<br>= = = = = = = = = = = = = = = = = = = = = =<br>
After:<br>parse_str(\"$str\", \$res)<br><br>";
var_dump(parse_str($str, $res));
echo "<br><br>\$res = <br>";
var_dump($res);
$str = "arr1]=sid&arr[4]2]=fred";
echo "<br><br>- - - - - - -<br>
After:<br>parse_str(\"$str\", \$res)<br><br>";
var_dump(parse_str($str, $res));
echo "<br><br>\$res = <br>";
var_dump($res);
$str = "arr[one=sid&arr[4][two=fred";
echo "<br><br>- - - - - - -<br>
After:<br>parse_str(\"$str\", \$res)<br><br>";
var_dump(parse_str($str, $res));
echo "<br><br>\$res = <br>";
var_dump($res);
echo "<br>- - - - - - -";
echo "<br><br><br>Test string with badly formed % numbers.";
$str = "first=%41&second=%a&third=%b";
echo "<br>= = = = = = = = = = = = = = = = = = = = = = = = =<br>
After:<br>parse_str(\"$str\", \$res)<br><br>";
var_dump(parse_str($str, $res));
echo "<br><br>\$res = <br>";
var_dump($res);
echo "<br><br>Test string with non-binary safe name.<br>";
$str = "arr.test[1]=sid&arr test[4][two]=fred";
echo "= = = = = = = = = = = = = = = = = = = = = = =<br>After:<br>parse_str(\"$str\", \$res)<br><br>";
var_dump(parse_str($str, $res));
echo "<br><br>\$res = <br>";
var_dump($res);
echo "<br>- - - - - - -";
?>
<?php
function test() {
// strlen("abcd") == 4 is relevant
parse_str('abcd=1', $array);
var_dump($array);
}
test();
?>