From what I see, you have a valid serialized string nested inside of a valid serialized string -- meaning serialize()
was called twice in the formation of your posted string.
See how you have s:151:
followed by:
"a:1:{i:0;a:4:{s:4:"name";s:15:"Chloe O'Gorman";s:6:"gender";s:6:"female";s:3:"age";s:3:"3_6";s:7:"present";s:34:"Something from Frozen or a jigsaw ";}}";
⮤ that is a valid single string that contains pre-serialized data.
After you unserialize THAT, you get:
a:1:{i:0;a:4:{s:4:"name";s:15:"Chloe O'Gorman";s:6:"gender";s:6:"female";s:3:"age";s:3:"3_6";s:7:"present";s:34:"Something from Frozen or a jigsaw ";}}
// ^^--^^^^^^^^^^^^^^-- uh oh, that string value has 14 bytes/characters not 15
It looks like somewhere in the string processing, and escaping slash was removed and that corrupted the string.
There is nothing foul about single quotes in serialized data.
You can choose to either:
- execute an escaping call to blindly apply slashes to ALL single quotes in your string (which may cause breakages elsewhere) -- assuming you WANT to escape the single quotes for your project's subsequent processes or
- execute my following snippet which will not escape the single quotes, but rather adjust the byte/character count to form a valid
Code: (Demo)
$corrupted_byte_counts = <<<STRING
s:151:"a:1:{i:0;a:4:{s:4:"name";s:15:"Chloe O'Gorman";s:6:"gender";s:6:"female";s:3:"age";s:3:"3_6";s:7:"present";s:34:"Something from Frozen or a jigsaw ";}}";
STRING;
$repaired = preg_replace_callback(
'/s:\d+:"(.*?)";/s',
function ($m) {
return 's:' . strlen($m[1]) . ":\"{$m[1]}\";";
},
unserialize($corrupted_byte_counts) // first unserialize string before repairing
);
echo "corrupted serialized array:\n$corrupted_byte_counts";
echo "\n---\n";
echo "repaired serialized array:\n$repaired";
echo "\n---\n";
print_r(unserialize($repaired)); // unserialize repaired string
echo "\n---\n";
echo serialize($repaired);
Output:
corrupted serialized array:
s:151:"a:1:{i:0;a:4:{s:4:"name";s:15:"Chloe O'Gorman";s:6:"gender";s:6:"female";s:3:"age";s:3:"3_6";s:7:"present";s:34:"Something from Frozen or a jigsaw ";}}";
---
repaired serialized array:
a:1:{i:0;a:4:{s:4:"name";s:14:"Chloe O'Gorman";s:6:"gender";s:6:"female";s:3:"age";s:3:"3_6";s:7:"present";s:34:"Something from Frozen or a jigsaw ";}}
---
Array
(
[0] => Array
(
[name] => Chloe O'Gorman
[gender] => female
[age] => 3_6
[present] => Something from Frozen or a jigsaw
)
)
---
s:151:"a:1:{i:0;a:4:{s:4:"name";s:14:"Chloe O'Gorman";s:6:"gender";s:6:"female";s:3:"age";s:3:"3_6";s:7:"present";s:34:"Something from Frozen or a jigsaw ";}}";
*keep in mind, if you want to return your data to its original Matryoshka-serialized form, you will need to call serialize()
again on $repaired
.
**if you have substrings that contain ";
in them, you might try this extended version of my snippet.