Notes for using UTF-8 in PHPer

Source: Internet
Author: User
The function of this paper is to avoid the garbled problem caused by the use of UTF-8 encoding in an all-round way, does not contain the specific garbled solution and the content of encoding conversion. For a deep understanding of UTF-8 encoding, see: "PHP matching UTF-8 Chinese characters regular expression the role of this article is to avoid the use of UTF-8 encoding caused by garbled problem, it does not contain specific garbled solutions and encoding conversion content. For more information about UTF-8 encoding, see PHP regular expressions matching Chinese characters in UTF-8
1. editor: Do not use any Microsoft editor, what front page, Web Designer, notepad, WordPad or something, can be lost all open, because these editors will generate BOM in front of your UTF-8 document, BOM specific instructions, can be found here, when I directly take notepad to cover the original file, resulting in a large number of code damage, the memory is still new.
2. MySQL database: When you create a database, create a table, and create fields (note that there are three fields), you must select the utf8_general_ci sorting format (Collation) and use PHP in php? Name = mysql "onclick =" tagshow (event) "class =" t_tag "> after the mysql_connect () function connects data, add the following sentence:
Mysql_query ('set names "utf8 "');

3. PHP:
Use the mbstring library instead of the iconv library.
Use preg instead of ereg to process characters.
When using the htmlentities () function, the html_entity_decode () function must carry the third parameter: copy the content to the clipboard code: $ Str = "Chinese ";
Echo htmlentities ($ str, ENT_COMPAT, "UTF-8"); // Display (inside the source code) Juan? Bytes
Echo html_entity_decode (htmlentities ($ str, ENT_COMPAT, "UTF-8"), ENT_COMPAT, "UTF-8"); // Display (inside the source code) Chinese
?> Here is a regular expression that matches the Chinese characters of the UTF-8: copy the content to the clipboard code: $ Word = "Chinese ";
If (preg_match ("/^ ([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {1}/", $ word) = true | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {1} $/", $ word) = true | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {2,}/", $ word) = true)
{
Echo "Well, it's a UTF-8-encoded Chinese character ";
}
Else
{
Echo "Sorry, this is not a UTF-8-encoded Chinese character ";
}
?> 4. HTML: inAdd tags


5, JS: JS problems are more complex, if the file is stored as a UTF-8 code, so when the user directly access this page with a browser (maybe some programmers will do), the browser may use other encoding to read this file and cause the UTF-8 Chinese garbled, of course, this does not affect the running of the program. When loading JS, you can describe the JS encoding. of course, if the encoding has been declared in the HTML header, it is not necessary here:

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.