Regex/ code to fix corrupt serialized PHP data.

前端 未结 12 714
夕颜
夕颜 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:24

    Using preg_replace_callback(), instead of preg_replace(.../e) (because /e modifier is deprecated).

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

提交回复
热议问题