Wednesday 26 October 2016

php - How to convert a string into a readable view




From some database i get string, it looks like this



[5] => Array
(
[0] => 6
[body] => ÉÓËÒÅÎÎÅ ÔÅÂÅ ÖÅÌÁÀ ã×ÅÔÏ×, ÌÀÂ×É É ËÒÁÓÏÔÙ!
)


How to convert this string into a readable view using php?

I'm use PDO type connection with option "SET NAMES utf8", maybe I need to change this option?






Using phpmyadmin i see this table decription



table description



and browse




table view


Answer



Lazy solution: set names to latin1 (or don't set it at all, as it's set by default).



It will return you results in original cp1251, which you can convert in any other encoding using iconv() or mb_convert_encoding().



Complex but proper solution: set the right encoding for the table columns. It have to be cp1251, not latin1. For this, you have to re-create tables using CHARACTER SET=1251 in table definition and then re-filling tables back with data.



After that everything will be back in order:





  • phpmyadmin will show your data correctly

  • you will be able to set names to utf8 and get data in this encoding

  • (most important part) WHERE and ORDER BY operators will stop driving you crazy.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...