Degree '°' character not displaying in php json_encode function, how to display this?

后端 未结 4 1468
臣服心动
臣服心动 2021-02-19 21:59

I pass php array like

[sit_latitude] => 20° 23.298\' N

inside json_encode(); and output is like,

  {\"sit_latitude\":null}
<         


        
相关标签:
4条回答
  • 2021-02-19 22:47

    You could try htmlspecialchars() function http://php.net/manual/en/function.htmlspecialchars.php

    0 讨论(0)
  • 2021-02-19 22:53

    json_encode assumes that strings in its input are valid UTF-8 (keep in mind that PHP strings are sequences of bytes, not sequences of Unicode code points). If your text editor is not set to use that encoding (and you haven't manually generated the UTF-8 using hex escape codes such as \xc2\xb0 in a double-quoted string), the string will be encoded as null (because it is not valid UTF-8).

    Other common charsets such as Windows-1252 are also a superset of ASCII, so this is only a problem if there are any non-ASCII characters (U+00B0 DEGREE SIGN included).

    Even if the string is valid UTF-8, by default, json_encode will output \u00b0 instead of the actual degree sign. This is not a problem for programmatic decoding using PHP json_decode or JavaScript JSON.parse, which understand JSON hex escape sequences.

    (In PHP 5.4, there is a JSON_UNESCAPED_UNICODE flag to prevent this escaping. However, many web servers still have PHP 5.3 or older, and the flag unescapes even the problematic characters U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR. It is possible to simulate the flag in two lines of code, although as I explained above, you don't have to anyway.)

    0 讨论(0)
  • 2021-02-19 22:54

    JSON and escaping characters - this post may help you.

    Just pass degree as \u00f8C and then decode it with js.

    0 讨论(0)
  • 2021-02-19 22:59

    what i tried and succeed was bit strange but worked , so let me share it with you.

    $latitude = json_encode(utf8_encode($emplyoee['sit_latitude']));
    $longitude = json_encode(utf8_encode($employee['sit_longitude']));
    
       $emplyoee['sit_latitude'] = $latitude;
        $emplyoee['sit_longitude'] = $longitude;
    

    and now pass like

    echo '{"item":'. json_encode($employee) .'}';
    

    this will solve your problem

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