php - A string processed by urldecode is used in an array element, causing json_encode to be unable to convert the array into a json string. How to solve the problem?
淡淡烟草味
淡淡烟草味 2017-05-24 11:33:54
0
3
639
$str = 'Success is 1% inspiration and %99 perspiration';
$urldecodeStr = urldecode($str);
$arr = [
        'doc' => $urldecodeStr
    ];
echo $urldecodeStr; //输出: Success is 1% inspiration and � perspiration
echo "<br>";
var_dump(json_encode($arr));//返回false
  1. What is the reason why json_encode cannot be converted?

  2. Is there any way to make the array $arr using $urldecodeStr be normally encoded into a json string by json_encode?

淡淡烟草味
淡淡烟草味

reply all(3)
Peter_Zhu

Question

1.json_encode cannot be converted due to encoding problems. When $str is converted to u8, %99 is a garbled code.
2. At present, I don’t know what the hell you are getting %99.
3. After you urldecode, the output will be garbled

Solution

1. Use the iconv function to transcode $str
2. Remove %99 (of course this is up to you)

phpcn_u1582

Why directly urldecode the unencoded string

滿天的星座

There may be something wrong with the string. . . In principle, json only supports UTF-8 encoding

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template