When dealing with character encoding in PHP, the first thing you need to do is to synchronize your PHP pages with your database. Your pages must have the same character encoding of your database. So if your database is in UTF-8, your pages must be in UTF-8 too. To accomplish this task, you should never rely on the HTML meta
elements. These elements simply tells the browser that your page should be interpreted in a given encoding, not that your page is actually encoded in that encoding. So you should first check if your PHP editor correctly encodes your pages. Do this simple test:
- create a blank page without any
meta
element, for example<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- save your page in the desired encoding (here UTF-8)
- open your page in a web browser and check the page info section
- if your browser says that the page is in UTF-8, then everything worked fine
- otherwise, your PHP editor doesn't save your pages in the specified encoding.
Most PHP editors claim that they save your pages in UTF-8, but they actually don't do that. What's next then?
- use the
utf8_encode()
andutf8_decode()
PHP functions - use the
iconv()
PHP function
These functions should be used either when you save something on your database or when you retrieve something from it. If you properly escape user's input, then there won't be no problem at all. On the contrary, when displaying characters from the database, you can use the solution proposed by Sergio in his post:
function fixEncoding($in_str) { $cur_encoding = mb_detect_encoding($in_str); if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8")) { return $in_str; } else { return utf8_encode($in_str); } }
Hi Gabriele,
Thank you and go-on with the beautiful work you're doing on your blog.
Best.