Created
December 20, 2016 17:54
-
-
Save lfbittencourt/994517d6cff9699217ffd884cb056e86 to your computer and use it in GitHub Desktop.
An unserialize wrapper that gets rid of "Error at offset" errors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This function fixes string lengths before unserializing a value, | |
* in case some content was found and replaced, for example. | |
* | |
* @param string $str | |
* @param array $options | |
* @return mixed | |
*/ | |
function unserialize_after_fixing($str, array $options = []) | |
{ | |
$str = preg_replace_callback('/s:(\d+):"(.*?)";/s', function ($matches) { | |
return 's:' . strlen($matches[2]) . ':"' . $matches[2] . '";'; | |
}, $str); | |
if (version_compare(PHP_VERSION, '7.0.0') >= 0) { | |
return unserialize($str, $options); | |
} | |
return unserialize($str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment