$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
What is the reason why json_encode cannot be converted?
Is there any way to make the array $arr
using $urldecodeStr
be normally encoded into a json string by json_encode?
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)
Why directly urldecode the unencoded string
There may be something wrong with the string. . . In principle, json only supports UTF-8 encoding