Created
July 31, 2011 23:33
-
-
Save danielcbaldwin/1117347 to your computer and use it in GitHub Desktop.
Function to check if a string is actually serialized data.
This file contains 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
/** | |
* Checks if data is serialized | |
* @param mixed $data | |
*/ | |
public function is_serialized($data) { | |
if (!is_string( $data)) { | |
return false; | |
} | |
$data = trim($data); | |
if ('N;' == $data) { | |
return true; | |
} | |
if (!preg_match('/^([adObis]):/', $data, $badions)) { | |
return false; | |
} | |
switch ($badions[1]) { | |
case 'a' : | |
case 'O' : | |
case 's' : | |
if (preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data)) { | |
return true; | |
} | |
break; | |
case 'b' : | |
case 'i' : | |
case 'd' : | |
if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data)) { | |
return true; | |
} | |
break; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment