0

I'm using CakePHP to output an array that contains several UTF-8 encoded strings. I have a layout set up for the output (it's a REST API method):

<?php.
  header("Pragma: no-cache");.
  header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");.
  header('Content-Type: application/json; charset=UTF-8');.
  header("X-JSON: ".$content_for_layout);.
  echo $content_for_layout;.
?>

This is my view:

<?php echo json_encode($items); ?> 

My database table where I get the data is encoded in utf-8. But when I output the data if one of its elements has special characters like à, á, etc, the string will be set to null in the JSON array. How can I properly output my data?

3 Answers 3

2

It looks like your database connection is not set to utf-8, which is the most important part. So add 'encoding' => 'utf8' to the database configuration in your app/config/database.php, for example:

    'default' => array(
        'driver'   => 'mysql',
        'host'     => 'YOURHOST',
        'login'    => 'YOURLOGIN',
        'password' => 'YOURPASS',
        'database' => 'YOURDB',
        'encoding' => 'utf8'
    ),

If you don't set the encoding in the connection, a "default" encoding will be used. The default is likely not utf8.

Sign up to request clarification or add additional context in comments.

6 Comments

Let me check, though there is a problem, some tables are set to latin1 and others to utf8, won't this generate some inconsistency? (don't judge me :( I didn't set up the DB or the site)
Just checked and the encoding is set to utf-8, any difference with utf8?
@8vius MySQL calls it "utf8", not "utf-8". The application has been cruising along with broken encoding settings so far...
There won't be any inconsistency; MySQL will return all text in the connection encoding. The latin1 text will also be returned in UTF-8. If this causes display errors check that you have set App.encoding to utf-8 in config/core.php.
It's set and I am getting display errors, like mentioned in the comments to @deceze's answer.
|
1

That most likely means your data is not encoded in UTF-8, most likely because the database connection is not set to UTF-8 and you're actually receiving the data in latin1 on the PHP side. See Handling Unicode Front To Back In A Web App for a rundown of all gotchas.

11 Comments

My database table is set to utf8_general_ci, how can I do the proper setup for CakePHP? Though I really shouldn't because I might break the entire site (I didn't develop it, I'm just adding an API method)
@Joni just provided the answer. If you cannot change that default though (indeed, if it has been used without a proper connection setting so far your data will screw up royally if you change it), you'll have to convert the encoding of the strings manually as shown in the other answers. Why that doesn't work for you I don't know. Show us a bin2hex($string) of one of the strings so we can see what encoding they seem to be in.
Just did the change that @Joni said, now I get the string with the encoding of the characters like \u00f3, and in the view it's broken and shows stuff like ó
@8vius Yup, because the data in the database is already garbage. Why is explained in detail in my linked article... :)
In the database, if I look at it in phpMyAdmin it displays properly with the special characters and all.
|
0
$items = json_encode( array_map( function($text){ return is_string($text) ? utf8_encode($text) : $text; }, $items) )
echo $items;

1 Comment

Same error, the data from the database is alreadt utf-8 encoded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.