Recover Truncated PHP Serialized Arrays
Shaun Inman has posted a great function that will salvage all the data that it can in a serialized array that has been truncated. PHP's unserialize() function will fail if it encounters a malformed string. This is where repairSerializedArray() function comes in to save the day.
PHP’s native serialization text format is the simplest way to store an arbitrary array in a database without losing the types of its many values or their structure within the array. However, you might occasionally run into problems when the serialized string is longer than the database column that houses it. The resulting truncated string cannot be unserialized by PHP. The majority of the data might still be intact but PHP doesn’t know what to do with it; so I wrote a function, two actually, that does. (While PHP’s serialize() and unserialize() functions also work with objects this recovery function does not.)
Because the recursive function operates on and whittles down the actual serialized string while it attempts its recovery, a second function duplicates and prepares the string leaving the original unchanged.
Code Example
-
// the native unserialize() function returns false on failure
-
if ($data === false) // could not unserialize
-
{
-
$data = repairSerializedArray($serialized); // salvage what we can
-
}
You can read the full post and download the code here

My name is Noah Everett. I live in Tulsa, OK. I started 