note 81783 deleted from function.unserialize by betz
| From: | betz@php.net | Date: | Thu, 13 Mar 2008 12:56:20 +0000 |
| Subject: | note 81783 deleted from function.unserialize by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137014@lists.php.net to get a copy of this message | ||
Note Submitter: dev at atshop3d dot com
----
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>";
?>