Created
August 1, 2011 02:27
-
-
Save SimonEast/1117476 to your computer and use it in GitHub Desktop.
PHP Code to detect serialized string and recursively unserialize
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 | |
// Next two functions taken from a commenter on http://php.net/manual/en/function.unserialize.php | |
function unserialize_recursive($val) { | |
//$pattern = "/.*\{(.*)\}/"; | |
if(is_serialized($val)){ | |
$val = trim($val); | |
$ret = unserialize($val); | |
if (is_array($ret)) { | |
foreach($ret as &$r) $r = unserialize_recursive($r); | |
} | |
return $ret; | |
} elseif (is_array($val)) { | |
foreach($val as &$r) $r = unserialize_recursive($r); | |
return $val; | |
} else { return $val; } | |
} | |
function is_serialized($val) { | |
if (!is_string($val)) return false; | |
if (trim($val) == "") return false; | |
$val = trim($val); | |
if (preg_match('/^(i|s|a|o|d):.*{/si', $val) > 0) return true; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful! Thank you!