Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of utf-8 coding in PHP, utf8 coding, database garbled, page display output garbled

Shulou Source: shulou.com Published: 2022-06-03 04:56:43 09月15日 Update

Editor to share with you the problem of utf-8 coding in PHP, utf8 coding, database garbled, page display output garbled how to solve, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. When building a database, especially when dealing with MYSQL with phpMyAdmin, it is usually utf-8 and the field is utf8_general_ci

Settings for the database:

Look in the my.ini file for:

[mysql]

Default-character-set = utf8

[mysqld]

Default-character-set = utf8

Init_connect = 'SET NAMES utf8'

All set to utf8

Save, restart the mysql service

two。 Mysql_query ("set names' utf8'") when dealing with data; note: utf8, not utf-8

3.PHP file default encoding is ANSI, need to be converted to UTF-8, as for how to convert editplus has such a function, "Save as" when there is a choice of encoding UTF-8, note: can not choose: UTF-8 + BOM, if you choose this, you will have problems when dealing with session, so be sure to pay attention. Some people develop in eclipse,Myeclipse,ZendStudio. The default in eclipse is ISO-8859-1. You need to open the preferences window in "window"-> "preferences", in "General"-> "Workspace" on the left, and set the default encoding to utf-8 in "text File Encoding".

4. It should be stated in the PHP file, such as

Or

The copy code is as follows:

5. In addition, there may be garbled codes when dealing with double bytes such as Chinese. Iconv,mb_convert_encoding can be used to deal with double bytes in PHP, and the rest can be referred to the PHP help manual.

6. In addition (one missing point), in your program, you need to know that there may also be coding problems in the transmission of data, but you do not know what kind of coding is used in the transmitted data. The method is provided in PHP to deal with it. Here is a simple method written by yourself, which you can refer to.

The copy code is as follows:

/ / Transcoding

Function display_fileencoding ($filename)

{

If (extension_loaded ("mbstring"))

{

$code=mb_detect_encoding ($filename); / / detect string encoding

$filename=mb_convert_encoding ($filename, "UTF-8", $code); / / convert the encoding $code to utf-8 encoding

Return $filename

}

Else

Die ("Please check that the system is properly installed and configured mbstring")

}

Make sure mbstring is enabled in your php.ini

The above is "utf-8 coding problems in PHP, utf8 coding, database garbled, page display garbled how to solve" all the contents of this article, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Coding data time garbled database problem code file UTF-8 processing utf-8 article reference selection page output content byte that is method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Redmi macOS Apple vpn