Any way to return PHP `json_encode` with encode UTF-8 and not Unicode?
{"a":"\u00e1"}
and {"a":"á"}
are different ways to write the same JSON document; The JSON decoder will decode the unicode escape.
In php 5.4+, php's json_encode
does have the JSON_UNESCAPED_UNICODE
option for plain output. On older php versions, you can roll out your own JSON encoder that does not encode non-ASCII characters, or use Pear's JSON encoder and remove line 349 to 433.
Why does the PHP json_encode function convert UTF-8 strings to hexadecimal entities?
Since PHP/5.4.0, there is an option called JSON_UNESCAPED_UNICODE
. Check it out:
https://php.net/function.json-encode
Therefore you should try:
json_encode( $text, JSON_UNESCAPED_UNICODE );
json_encode() non utf-8 strings?
Is there a way I can get json_encode() to work and display these characters instead of having to use utf8_encode() on all of my strings and ending up with stuff like "\u0082"?
If you have an ANSI encoded string, using utf8_encode()
is the wrong function to deal with this. You need to properly convert it from ANSI to UTF-8 first. That will certainly reduce the number of Unicode escape sequences like \u0082
from the json output, but technically these sequences are valid for json, you must not fear them.
Converting ANSI to UTF-8 with PHP
json_encode
works with UTF-8
encoded strings only. If you need to create valid json
successfully from an ANSI
encoded string, you need to re-encode/convert it to UTF-8
first. Then json_encode
will just work as documented.
To convert an encoding from ANSI
(more correctly I assume you have a Windows-1252
encoded string, which is popular but wrongly referred to as ANSI
) to UTF-8
you can make use of the mb_convert_encoding()
function:
$str = mb_convert_encoding($str, "UTF-8", "Windows-1252");
Another function in PHP that can convert the encoding / charset of a string is called iconv
based on libiconv. You can use it as well:
$str = iconv("CP1252", "UTF-8", $str);
Note on utf8_encode()
utf8_encode()
does only work for Latin-1
, not for ANSI
. So you will destroy part of your characters inside that 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 are available in the source code only so far).
Changing the encoding of an array/iteratively (PDO comment)
As you wrote in a comment that you have problems to apply the function onto an array, here is some code example. It's always needed to first change the encoding before using json_encode
. That's just a standard array operation, for the simpler case of pdo::fetch()
a foreach
iteration:
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 );
}
PHP json_encode json_decode UTF-8
This is an encoding issue. It looks like at some point, the data gets represented as ISO-8859-1.
Every part of your process needs to be UTF-8 encoded.
The database connection
The database tables
Your PHP file (if you are using special characters inside that file as shown in your example above)
The
content-type
headers that you output
Charset - json_encode with utf-8
When encoding unicode data with json_encode you should use the JSON_UNESCAPED_UNICODE
flag:
$json = json_encode($darr, JSON_UNESCAPED_UNICODE);
The above is available since php 5.4.0.
For older versions you can try and use this function instead:
function unicode_json_encode($arr) {
//convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding
array_walk_recursive($arr, function (&$item, $key) { if (is_string($item)) $item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8'); });
return mb_decode_numericentity(json_encode($arr), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');
}
The above function was taken from the comments in json_encode page in php.net
Related Topics
Switch PHP Versions on Commandline Ubuntu 16.04
PHP - How to Merge Arrays Inside Array
PHP How to Start an External Program Running - Having Trouble With System and Exec
What Is the Syntax For Accessing PHP Object Properties
Netbeans Shows "Waiting For Connection (Netbeans-Xdebug)"
Laravel - Eloquent or Fluent Random Row
PHP Regular Expression to Match Keyword Outside HTML Tag ≪A≫
How to Write to the Console in PHP
Alternative to MySQL_Real_Escape_String Without Connecting to Db
How to Embed a .Png Image into an HTML Page
How to Strip Trailing Zeros in PHP
What Does MySQL_Real_Escape_String() Do That Addslashes() Doesn'T
How to Change PHP Version in Htaccess in Server
Rebase Array Keys After Unsetting Elements
With "Magic Quotes" Disabled, Why Does PHP/Wordpress Continue to Auto-Escape My Post Data