Regex/ code to fix corrupt serialized PHP data.

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

    Best Solution for me:

    $output_array = unserialize(My_checker($serialized_string));

    code:

    function My_checker($serialized_string){
        // securities
        if (empty($serialized_string))                      return '';
        if ( !preg_match('/^[aOs]:/', $serialized_string) ) return $serialized_string;
        if ( @unserialize($serialized_string) !== false ) return $serialized_string;
    
        return
        preg_replace_callback(
            '/s\:(\d+)\:\"(.*?)\";/s', 
            function ($matches){  return 's:'.strlen($matches[2]).':"'.$matches[2].'";';  },
            $serialized_string )
        ;
    }
    

提交回复
热议问题