PHP unserialize fails with non-encoded characters?

后端 未结 14 1579
遥遥无期
遥遥无期 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:58

    One more slight variation here which will hopefully help someone ... I was serializing an array then writing it to a database. On retrieving the data the unserialize operation was failing.

    It turns out that the database longtext field I was writing into was using latin1 not UTF8. When I switched it round everything worked as planned.

    Thanks to all above who mentioned character encoding and got me on the right track!

    0 讨论(0)
  • 2020-11-27 16:59

    I know this was posted like one year ago, but I just have this issue and come across this, and in fact I found a solution for it. This piece of code works like charm!

    The idea behind is easy. It's just helping you by recalculating the length of the multibyte strings as posted by @Alix above.

    A few modifications should suits your code:

    /**
     * Mulit-byte Unserialize
     *
     * UTF-8 will screw up a serialized string
     *
     * @access private
     * @param string
     * @return string
     */
    function mb_unserialize($string) {
        $string = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $string);
        return unserialize($string);
    }
    

    Source: http://snippets.dzone.com/posts/show/6592

    Tested on my machine, and it works like charm!!

    0 讨论(0)
提交回复
热议问题