PHP unserialize fails with non-encoded characters?

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

    In reply to @Lionel above, in fact the function mb_unserialize() as you proposed won't work if the serialized string itself contains char sequence "; (quote followed by semicolon). Use with caution. For example:

    $test = 'test";string'; 
    // $test is now 's:12:"test";string";'
    $string = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $test);
    print $string; 
    // output: s:4:"test";string";  (Wrong!!)
    

    JSON is the ways to go, as mentioned by others, IMHO

    Note: I post this as new answer as I don't know how to reply directly (new here).

提交回复
热议问题