How to json_encode array with french accents?
json_encode
only wants utf-8
. Depending on your character set, you can use iconv
or utf8_encode
before calling json_encode
on your variable. Probably with array_walk_recursive
.
As requested, an unfinished way to alter an array, with the assumptions that (1) it doesn't contain objects, and (2) the array keys are in ascii / lower bounds, so can be left as is:
$current_charset = 'ISO-8859-15';//or what it is now
array_walk_recursive($array,function(&$value) use ($current_charset){
$value = iconv('UTF-8//TRANSLIT',$current_charset,$value);
});
How to JSON encode an array with French accents?
Try this,
json_encode($array, JSON_UNESCAPED_UNICODE);
You should get this result;
{
"dépendre":"to depend",
"dire":"to say",
"distraire":"distracted",
"être":"to be (being)"
}
json_encode won't encode French characters
I managed to figure it out. It's not really the solution I wanted but it works. I had to adjust my query to look like:
CONVERT(CAST(langselect as BINARY) USING latin1) as langselect
json_encode function: special characters
Your input has to be encoded as UTF-8 or ISO-8859-1.
http://www.php.net/manual/en/function.json-encode.php
Because if you try to convert an array of non-utf8 characters you'll be getting 0 as return value.
Since 5.5.0 The return value on failure was changed from null string to FALSE.
json_encode arrays with iso-8895 characters
What about something like this?
array_walk_recursive($array, function (&$elem) {
if (is_string($elem)) {
$elem = iconv('ISO-8895', 'UTF-8', $elem);
}
});
echo json_encode($array);
Accented letters with PHP: htmlentities and json_encode
You're not specifying which error you are getting in json_encode()
, but my suspicion is that your incoming data is not UTF-8 encoded, which will break both the unnecessary htmlentities()
call (which you are instructing to expect UTF-8 data) and json_encode()
(because that function requires all input data to be UTF-8).
Make sure the data you are encoding is actually UTF-8 encoded. You won't be needing htmlentites()
at all then.
You're not saying where the data comes from, but if it's from a database, the connection sometimes needs to be explicitly set to UTF-8. See UTF-8 all the way through
Null php json while econde a query from mySQL with accents
json_encode assumes that the result is encoded in UTF-8, if it is not returned null
first verify that your database server is configured with UTF_8or add the set_charset before the consultation
mysqli_set_charset($conn,"utf8");
Use header to modify the HTTP header, UTF-8 all the way through
header('Content-Type: text/html; charset=utf-8');
json with special characters like é
Just like the first anwser
Do you use a database? If Yes, make sure the database table is declared UFT8
How is declared the HTML page? UTF-8
IS the string in the PHP script file? If yes, make sure the file has a UTF-8 file format
You could also use utf8_encode (to send to HTML) and utf8_decode (to receive) but not the right way
Related Topics
Does HTML_Entity_Decode Replaces &Nbsp; Also? If Not How to Replace It
How to Resize and Convert an Uploaded Image to a Png Using Gd
Symfony: Form Issue Using Return Type Hinting in Doctrine Entity Methods
Mail Not Sending with PHPmailer Over Ssl Using Smtp
Ssl Alternative - Encrypt Password with JavaScript Submit to PHP to Decrypt
Convert Windows Timestamp to Date Using PHP on a Linux Box
Wordpress Paginate_Links - How to Use It
Create a Joomla! Article Programmatically
How to Return Only Named Groups with Preg_Match or Preg_Match_All
Call-Time Pass-By-Reference Has Been Removed
How to Wrap Text Using Imagick in PHP So That It Is Drawn as Multiline Text
How to Use a Class Method as a Callback Function
How to Get Content of Remote HTML Page
Xpath Get Attribute Value in PHP
How to Run a Whois Lookup with PHP or Python
How to Require PHP Files Relatively (At Different Directory Levels)