note 81783 added to function.unserialize
| From: | dev at atshop3d dot com | Date: | Thu, 13 Mar 2008 12:17:15 +0000 |
| Subject: | note 81783 added to function.unserialize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137008@lists.php.net to get a copy of this message | ||
Thanks to Igor who helped provide the solution to this. Serialize is very strict when it comes to
quotes, and I spent _days_ figuring out why MySQL/PostgreSQL/etc. and locally set serialized strings
returned a blank array, testing with both double and single quotes to see which one works.
Changing literal single quotes to "'" (I'm using numeric reference since
’ and ‘ are different) and double quotes to """ cures all
problems, while changing only one at a time only remedies a few of them. I'm interested to hear
if other people have different experiences.
For example, the following string works:
<?php
//--------------------------------------------
// Set the example value. It doesn't matter if the string is from
// a database query or locally set - the side-effects are the same.
$heightoptionslist = <<<HEIGHTEND
a:3:{s:37:"Hands/inches (eg. 13'
2"HH)";s:6:"option";s:25:"Inches only
(eg.39")";s:6:"option";s:24:"Centimeters (eg.
153cms)";s:6:"option";}
HEIGHTEND;
//--------------------------------------------
// Change the serialized string back to an array.
$heightoptionslist = unserialize($heightoptionslist);
//--------------------------------------------
// Output the value in a format we can understand.
echo "<div><pre>\$heightoptionslist = [\n".print_r($heightoptionslist,
true)."\n]</pre></div>";
?>
while a string containing literal quotes doesn't (in nearly all cases on this machine):
<?php
//--------------------------------------------
// Another example value,
// this time with escaped literal quotes
$heightoptionslist = <<<HEIGHTEND
a:3:{s:26:"Hands/inches (eg. 13\'
2\"HH)";s:6:"option";s:20:"Inches only
(eg.39\")";s:6:"option";s:24:"Centimeters (eg.
153cms)";s:6:"option";}
HEIGHTEND;
//--------------------------------------------
// Change the serialized string back to an array.
$heightoptionslist = unserialize($heightoptionslist);
//--------------------------------------------
// And return a blank array for some fun.
echo "<div><pre>\$heightoptionslist = [\n".print_r($heightoptionslist,
true)."\n]</pre></div>";
?>
----
Server IP: 117.104.160.194
Probable Submitter: 61.68.215.66
----
Manual Page -- http://www.php.net/manual/en/function.unserialize.php
Edit -- https://master.php.net/note/edit/81783
Del: integrated -- https://master.php.net/note/delete/81783/integrated
Del: useless -- https://master.php.net/note/delete/81783/useless
Del: bad code -- https://master.php.net/note/delete/81783/bad+code
Del: spam -- https://master.php.net/note/delete/81783/spam
Del: non-english -- https://master.php.net/note/delete/81783/non-english
Del: in docs -- https://master.php.net/note/delete/81783/in+docs
Del: other reasons-- https://master.php.net/note/delete/81783
Reject -- https://master.php.net/note/reject/81783
Search -- https://master.php.net/manage/user-notes.php