json_encode () output data just know UTF-8 ., all in the output data, pay attention to the data encoding format!!!
Solution:
If you have a ANSI encoded string, using Utf8_encode () is the wrong function to deal with this. ou need to properly convert it from ANSI to UTF-8 first. That would certainly reduce the number of Unicode escape sequences like \u0082 from the JSON output, but TECHN Ically These sequences is valid for JSON, you must not fear them.
Converting ANSI to UTF-8 with PHP
Json_encodeWorks with UTF-8Encoded strings only. If you need to create valid JsonSuccessfully from an Ansiencoded string, need to re-encode/convert it to UTF-8First. Then Json_encodeWould just work as documented.
To convert a encoding from Ansi(more correctly I assume a Windows-1252encoded string, which is popular and wrongly referred to as Ansi) to UTF-8You can make use of the Mb_convert_encoding ()function
$str = mb_convert_encoding ( $str , "UTF-8" , "Windows-1252" ); Another function in PHP this can convert the Encoding/charset of a string is called IconvBased Onlibiconv. You can use it as well:
$str = Iconv ( "CP1252" , "UTF-8" , $str ); Note on Utf8_encode ()
Utf8_encode ()Does for Latin-1, not for Ansi. So you'll destroy part of your characters inside this string when you run it through that function.
Related:what is ANSI format?
For a more fine-grained control of what Json_encode ()Returns, see the list of Predifined constants (PHP version dependent, incl. PHP 5.4, some constants remain undocumented and is available in the source code has so far).
Changing the encoding of an array/iteratively (PDO comment)
As you wrote in a comment so you had problems to apply the function onto an array, here is some code example. It ' s alwaysneeded to FirstChange the encoding before using Json_encode. That's just a standard array operation, for the simpler case of Pdo::fetch ()A ForeachIteration:
while ( $row = $q - Fetch ( PDO :: Fetch_assoc )) { foreach ( $row as & $value ) { $value = mb_convert_encoding ( $value , "UTF-8" , "Windows-1252" ); } unset ( $value ); # Safety:remove Reference $items [] = Array_map ( ' Utf8_encode ' , $row ); }
The problems encountered in the project are recorded for later use.
Literature:
Json_encode () Non utf-8 strings
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The above describes the PHP Json_encode data, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.