The string will be encoded, so you will have to do that with url_decode
.
Here is a function that I used to build a workable object out of the string.
function urldecode_to_obj($str) {
$str = trim($str, '"');
// explode string before decoding
foreach (explode('&', $str) as $chunk) {
$param = explode("=", $chunk);
if ($param) {
// search string for array elements and look for key-name if exists
preg_match('#\[(.+?)\]$#', urldecode($param[0]), $with_key);
preg_match('#\[\]$#', urldecode($param[0]), $no_key);
$mkey = preg_split('/\[/', urldecode($param[0]));
// converts to array elements with numeric key
if ($no_key) {
$data[$mkey[0]][] = urldecode($param[1]);
}
// converts to array elements with named key
if ($with_key) {
$data[$mkey[0]][$with_key[1]] = urldecode($param[1]);
}
if (!$no_key && !$with_key) {
$data[urldecode($param[0])] = urldecode($param[1]);
}
}
}
return (object)$data;
}
$str = "serialized_string";
$obj = urldecode_to_obj($str);
Your variables with values are now in the object. If you have a var1 variable in there with a value1:
$obj -> var1 = "value1";
You can also get thins back as an array. Just omit the (object) casting and return the data in t\he function as:
return $data;
If you want to return things to your JS then you should echo them out as an array and use json_encode:
$array = array("success" => true);
echo json_encode($array);