Fix serialized data broken due to editing MySQL database in a text editor?

后端 未结 6 609
青春惊慌失措
青春惊慌失措 2020-12-30 02:25

Background: I downloaded a *.sql backup of my WordPress site\'s database, and replaced all instances of the old database table prefix with a new on

6条回答
  •  既然无缘
    2020-12-30 03:15

    If the error is due to the length of the strings being incorrect (something I have seen frequently), then you should be able to adapt this script to fix it:

    foreach($strings as $key => $str)
    {
        try {
            unserialize($str);
        } catch(exception $e) {
            preg_match_all('#s:([0-9]+):"([^;]+)"#',$str,$m);
            foreach($m[1] as $k => $len) {
                if($len != strlen($m[2][$k])) {
                    $newstr='s:'.strlen($m[2][$k]).':"'.$m[2][$k].'"';
                    echo "len mismatch: {$m[0][$k]}\n";
                    echo "should be:    $newstr\n\n";
                    $strings[$key] = str_replace($m[0][$k], $newstr, $str);
                }
            }
        }
    }
    

提交回复
热议问题