Skip to content

Instantly share code, notes, and snippets.

@SimonEast
Created August 1, 2011 02:27
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SimonEast/1117476 to your computer and use it in GitHub Desktop.
Save SimonEast/1117476 to your computer and use it in GitHub Desktop.
PHP Code to detect serialized string and recursively unserialize
<?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;
}
@TheHiddenHaku
Copy link

Very useful! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment