Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP character encoding problems

I need help with a character encoding problem that I want to sort once and for all. Here is an example of some content which I pull from a XML feed, insert into my database and then pull out.

As you can not see, a lot of special html characters get corrupted/broken.

How can I once and for all stop this? How am I able to support all types of characters, etc.?

I've tried literally every piece of coding I can find, it sometimes corrects it for most but still others are corrupted.

like image 213
James Avatar asked Dec 05 '22 06:12

James


1 Answers

To absolutely once and for all make sure you will never have problems with encoding again:

Use UTF-8 everywhere and on everything!

That is (if you use mysql and php):

  • Set all the tables in your database to collation "utf8_general_ci" for example.
  • Once you establish the database connection, run the following SQL query: "SET NAMES 'utf8'"
  • Always make sure the settings of your editor are set to UTF-8 encoding.
  • Have the following meta tag in the section of your HTML documents:

    <meta http-equiv="content-type" content="text/html; charset=utf-8">

And couple of bonus tips:

  • When you use PHP for string manipulation, use the multibyte functions.
  • You might check http://docs.kohanaphp.com/core/utf8 as well at some point.

OR:

You can just use one simple server side configuration file that takes care of all encoding stuff. In this case you wont need header and/or meta tags at all or php.ini file modification. Just add your wanted character set encoding to .htaccess file and put it into your www root. If you want to fiddle with character set strings and use your php code for that - thats another story. Database collation must ofcourse be correct.

Footnote: UTF-8 is not the encoding solution its an a solution. It doesn't matter what character set/encoding one is using as long as the used environment has been taking to consideration.

like image 130
Petrunov Avatar answered Dec 08 '22 15:12

Petrunov