Home php How to recode Windows-1252 in UTF-8?

How to recode Windows-1252 in UTF-8?




sparkled one order, I write down in the database. If you write to the database as it is, then in the database it will be: ðž½½

tried to translate this text in the decoder displays: CP1252 → UTF-8 = decorated

The Russian text is not written to the database (you need the word “decorated”), and doodle are written.

1) I try to recode:

$ result = iconv ('windows-1252', 'utf8', "$ text");

The result is the same.
I read this way:

$ res = iconv ('windows-1252', 'windows-1251', "$ text");
$ res = iconv ('Windows-1251', 'UTF8', "$ RES");

The result is empty.

2) try differently:

iconv ('utf8', 'windows-1251', "$ text");

Result: Title
I look at what kind of encoding, result:

how we had to suffer
CP1252 → CP1251
It turned out the word: decorated

So the encoding does not change? How to change the encoding?

Answer 1, Authority 100%

This is the usual 1251 and not 1252 – very often all these automatic recognitions are confused by these two encodings, while really 1252 meets much less often.

Your question should be divided into two questions. First, it is right to convert text to the correct encoding (ICONV – the right solution, you yourself wrote); The second is correct to insert into the database. Here you already need to watch the connection encoding / database / server and there can be a lot of nuances. After all, you are writing the “base”, “in the database”, right?

First look to display the right page (file, debugger). Do not forget that the starting code page can also encode not UFT-8, but CP1251. And when displaying the page, headlines given by the server are important and prescribed in the Headerh.

Check the connection settings to the database. Sometimes you can install Set Names UTF8 if you are too lazy to deal with encoding questions.

Answer 2

Solving the problem in the mysql server encoding. By default Latin1. Change the encoding in the file:

sudo nano -w /etc/mysql/my.cnf

See the following lines at the end of the file:

Collation-Server = UTF8_GENERAL_CI

In general, the text is not needed to recode. So write to the database as it is.

Answer 3

For those who will face the same problem:

$ str = 'trill);
$ STR = ICONV ('UTF-8', 'CP1252', $ STR);
$ STR = ICONV ('CP1251', 'UTF-8', $ STR);
Echo $ str;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions