简体   繁体   中英

Charset is not working - Spanish characters

This is really driving me crazy. I'm building a spanish website (meaning a lot of latin characters) and I'm using Zend framework.

When I save a á it displays like á . I know is a charset encoding but I dont understand why.

My head charset is <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> My database is utf8_general_ci . I've changed the charset to others and always the same problem.

When I look in my database, what is saved is an á

Any idea why is this happening? Thanks!

Here goes the recipe to get rid of encoding headaches:

  1. Your DB, tables and fields must use collation utf8_unicode_ci .
  2. Be sure your code (HTML, PHP... all ) is UTF-8 encoded.
  3. Always use this meta tag: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. This is long:

If you have access to MySQL file configuration my.cnf just add this line:

init-connect='SET NAMES utf8'

This tells MySQL to return results in UTF-8 for each connection.

If you cannot edit my.cnf but you are using PDO, open the connection this way:

$pdo = new PDO($dsn, $usr, $pwd, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

If you aren't using PDO... start using it, what are you waiting for? Meanwhile you can execute this after each connection if you use ext/MySQLi:

$mysql->set_charset('utf8');

Or this if you use plain old ext/MySQL:

mysql_set_charset('utf8', $connection);

The character á and other characters outside the ASCII map can be mapped with unicode .

As you are stroing the data in a table with UTF-8 charset and characters are storing properly.

I would suggest you that

  • Before you store data
  • Before you fetch data

Run the query SET NAMES UTF-8 . This will fetch your UTF-8 data with taking care of the characters which are outside of ASCII map.

While outputting the data you make sure that you are forcing/directing browser to use the UTF-8 charset to print the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM