Changing PHP character set to UTF-8 Method

Source: Internet
Author: User
Tags file handling ultraedit

Modify character sets, including php pages, html headers, files, database character sets, and database connections.
Address: http://blog.chinaunix.net/u1/41916/showart_470419.html
I don't know if he wrote it, but I still indicated the source I found.

1. Use vi/etc/httpd/conf/httpd. conf to set the language family in Apache (remember to restart)
Adddefacharcharset UTF-8

2. Use vi/etc/php. ini to set the language family in php (remember to restart)
Default_charset = "UTF-8"

3. Use vi/etc/my. cnf to set the MySQL language to (restart)
[Mysqld]
Init_connect = 'set NAMES utf8'
Default-character-set = utf8
[Client]
Default-character-set = utf8

4. Select the language family when creating a database: (remember to clear the database Cache)
Drop database if exists 'a ';
Create database 'A' default character set utf8 COLLATE utf8_unicode_ci;
USE 'a ';

Create table if not exists 'AAT '(
'Id' char (1) not null default '1 ',
'Mystr' varchar (200) default NULL,
Primary key ('id ')
) ENGINE = MyISAM default charset = utf8 COLLATE = utf8_unicode_ci;

5. Use UltraEdit (v11.20a) to convert all php files in ANSI format to UTF-8 format:
File --> Conversions --> ASCII to UTF-8 (Unicoding Editing)
(In UltraEdit, press Advanced --> configuration --> File Handling.
--> Unicode/UTF-8 Detection --> tick Auto detect UTF-8 files)
If necessary, you can execute Remove BOM. php. When using WinXP Notepad to convert php files from ANSI to UTF-8,
Because the Document Header has a BOM, it may cause typographical issues, so it must be removed. Execute Remove BOM. php to automatically Remove it.
Remove BOM. php can be downloaded from the following URL:
Http://www.hoyo.idv.tw/hoyoweb/document/view.php? Sid = 13 & author = hoyo & status = view

6. You must add the following to the php file:
<Html> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
</Head> <body>

7. Three rows of mysql_query must be added to the database to connect to the database. OK:
$ Host = "localhost"; $ DBname = "aa ";
$ User = "root"; $ passwd = "";
$ Link = mysql_connect ($ host, $ user, $ passwd) or die ("Fail ");
$ Db = mysql_select_db ($ DBname, $ link) or die ("Fail ");
// Add the following three rows before retrieving data in the real query database:
Mysql_query ("set names 'utf8 '");
Mysql_query ("SET CHARACTER_SET_CLIENT = utf8 ");
Mysql_query ("SET CHARACTER_SET_RESULTS = utf8 ");
$ SQL = "select * from aat where crid = '1 '";
$ Rows = mysql_query ($ SQL );

8. In the php file, note the following if necessary: [Optional]
When using htmlentities and htmlspecialchars, it should look like the following:
$ Chars = htmlentities ($ chars, ENT_QUOTES, "UTF-8 ");
$ Chars = htmlspecialchars ($ chars, ENT_QUOTES, "UTF-8 ");
And must be used before display
$ Chars = html_entity_decode ($ chars, ENT_QUOTES, "UTF8 ");
If you have used addslashes () or mysql_real_escape_string (), remember to use the following:
$ Chars = stripslashes ($ chars );
You can use the following function to convert different codes as needed:
$ Chars = iconv ('big5', 'utf-8', $ chars); // convert from Big5 to UTF-8

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<? Php header ('content-Type: text/html; charset = UTF-8 ');?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.