Changing character encoding in MySQL, PHP scripts, HTML

So, I created this system a long time ago and is currently outputting Latin1 (ISO-8859-1) to a web browser, and these are the components:

MySQL - all data is stored with the Latin1 character set

PHP - all PHP text files are stored on a Latin1 encoded disk

HTML The output is a meta tag http-equiv = "content-type" content = "text / html; charset = iso-8859-1"

So, I'm trying to understand how the coding of different parts comes into play in my workflow. If I open a PHP script and change its encoding in a text editor to UTF-8 and save it to disk and reload the web browser, the text will be corrupted - if the text does not come from the database. If I changed the database encoding to UTF-8 and saved the PHP files in latin1, I must use utf8_decode () so that the data is displayed correctly. And if I change the HTML code, the browser will not read it correctly.

So, I understand that if I want to “upgrade” to UTF8, I need to update all three parts of this installation so that it works correctly, but since it is a huge system with 180k lines of PHP code and millions of messages in many databases / tables, I I don’t want to start something like this without understanding everything correctly.

What didn’t I think about? What could hinder this decision? What are the procedures for changing the encoding of an entire MySQL installation and what is an easy way to change the encoding of hundreds or thousands of PHP files on disk?

For example, the META label is added dynamically, so I will change it only in one place :)

Let me tell you about your experience with this.

+3
source share
2 answers

It's complicated.

You should:

  • change the database and each character set / table encoding - I know little about MySQL, but see here
  • set client encoding to UTF-8 in PHP ( SET NAMES UTF8) before the first request
  • change the meta tag and possibly the Content-type header (note that the Content-type header takes precedence)
  • PHP UTF-8 - iconv.
  • : . strlen, mb_substr substr $str[index] .. mb_strlen ..21 >
+2

UTF8, . .
UTF8 () , .


. latin1 :

 echo htmlentities($string);

UTF8 :

 echo htmlentities($string, ENT_COMPAT, 'UTF-8');

strlen(), substr() .. .

MySQL
mysql_set_charset('UTF8') mysql_query('SET NAMES UTF8') UTF8, (SELECT). (INSERT, UPDATE) UTF8 .

, latin1 .
(, ☃, iPhone- ..) latin1. ( )


php-/.
ë ë .. , , latin1 utf8.

-1

Source: https://habr.com/ru/post/1748820/


All Articles