Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report