Regex/ code to fix corrupt serialized PHP data.

前端 未结 12 717
夕颜
夕颜 2020-11-30 07:04

I have a massive multidimensional array that has been serialised by PHP. It has been stored in MySQL and the data field wasn\'t large enough... the end has been cut off... I

12条回答
  •  暖寄归人
    2020-11-30 07:34

    Based on @Emil M Answer Here is a fixed version that works with text containing double quotes .

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

提交回复
热议问题