Regex/ code to fix corrupt serialized PHP data.

This is recalculating the length of the elements in a serialized array:

$fixed = preg_replace_callback(
    '/s:([0-9]+):\"(.*?)\";/',
    function ($matches) { return "s:".strlen($matches[2]).':"'.$matches[2].'";';     },
    $serialized
);

However, it doesn’t work if your strings contain ";. In that case it’s not possible to fix the serialized array string automatically — manual editing will be needed.

Leave a Comment