Regex/ code to fix corrupt serialized PHP data.

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

    Solution:

    1) try online:

    Serialized String Fixer (online tool)

    2) Use function:

    unserialize( serialize_corrector($serialized_string ) ) ;

    code:

    function serialize_corrector($serialized_string){
        // at first, check if "fixing" is really needed at all. After that, security checkup.
        if ( @unserialize($serialized_string) !== true &&  preg_match('/^[aOs]:/', $serialized_string) ) {
            $serialized_string = preg_replace_callback( '/s\:(\d+)\:\"(.*?)\";/s',    function($matches){return 's:'.strlen($matches[2]).':"'.$matches[2].'";'; },   $serialized_string );
        }
        return $serialized_string;
    } 
    

    there is also this script, which i haven't tested.

提交回复
热议问题