Stop UTF8 problems with special characters

I’m italian and I always need to use special chars like è é à ù. The same need belongs to…

Novembre 9, 2009

I’m italian and I always need to use special chars like è é à ù. The same need belongs to french developers, and many other non-english developers. To use special chars without encoding problems try to follow this 4 rules:

  1. create fields on db with utf8_general_ci collation
  2. make the query ‘set names’ after any connection (look the block code below, you need mysql 5)
  3. put meta charset utf8 tag in the html head block
  4. read and write strings to db and to html without using utf8_encode or other functions
  //after connecting to your db make this query:
  mysql_query("SET NAMES 'utf8';");

NOTE (added on 2010-02-16)
Sometimes these steps do not work, and you start say f**ck f**ck! Well, it’s also happened to me, and I’ve notice that sometimes php.ini make some problems because it has a wrong default charset setted. So, if this is your problem, put this line at the beginning of your php files:

ini_set('default_charset', 'UTF-8');

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

Comments on “Stop UTF8 problems with special characters”

2 thoughts

  1. ghazal ha detto:

    Hi,
    I can’t thank you enough for this little trick. I have been stuck for months (yes, months !) with an accented characters pb (in french) involving a datagrid/PHP/a JS framework and json encode and this neat trick solved it in a breeze. You made my day today.
    THANK YOU !!!!!

  2. SURIÑA ha detto:

    OH MY GOD
    IT WORKS!!!!!!

Comments are closed

Recommended

PHP function to fix collation on database fields of MySQL

This PHP function search for tables in the active db, match table’s name with the regular expression passed as first…

Febbraio 11, 2010

MYSQL add counter in a query

Use mysql variables to create a counter in SQL, PHP code to use an SQL counter and what does the i mean in mysqli?

Novembre 9, 2019

Modify the language attribute based on category in WordPress

How to modify the language attribute in your Wordpress theme using a specific value

Novembre 7, 2019

Embedding images in HTML or CSS with PHP

This small function returns an encoded string to embed images, inline, inside your html/css code and reduce the number of…

Dicembre 4, 2013

MySQL fulltext search always empty

If you’ve just switched to FULLTEXT indexes and you’ve just started playing with MATCH... AGAINST syntax, but you always get…

Novembre 23, 2013

How to remove custom fields from WordPress

Ok. You’ve added custom fields in wordpress, but how can you remove them? How to delete custom fields wordpress Two…

Settembre 29, 2013