PHP unserialize fails with non-encoded characters?

后端 未结 14 1769
遥遥无期
遥遥无期 2020-11-27 16:13
$ser = \'a:2:{i:0;s:5:\"héllö\";i:1;s:5:\"wörld\";}\'; // fails
$ser2 = \'a:2:{i:0;s:5:\"hello\";i:1;s:5:\"world\";}\'; // works
$out = unserialize($ser);
$out2 = un         


        
14条回答
  •  囚心锁ツ
    2020-11-27 16:39

    this one worked for me.

    function mb_unserialize($string) {
        $string = mb_convert_encoding($string, "UTF-8", mb_detect_encoding($string, "UTF-8, ISO-8859-1, ISO-8859-15", true));
        $string = preg_replace_callback(
            '/s:([0-9]+):"(.*?)";/',
            function ($match) {
                return "s:".strlen($match[2]).":\"".$match[2]."\";"; 
            },
            $string
        );
        return unserialize($string);
    }
    

提交回复
热议问题