search

Home  >  Q&A  >  body text

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?

$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?

淡淡烟草味淡淡烟草味2803 days ago647

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-05-24 11:35:54

    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)

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-24 11:35:54

    Why directly urldecode the unencoded string

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-24 11:35:54

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

    reply
    0
  • Cancelreply