PHP a variety of applications to solve the problem of garbled _php tutorial

Source: Internet
Author: User
1) Use the tag to set the page encoding

The purpose of this tag is to declare the client's browser with what character set encoding to display the page, XXX can be GB2312, GBK, UTF-8 (unlike MySQL, MySQL is UTF8) and so on. As a result, most pages can use this method to tell the browser what code to use when displaying this page, so that it does not cause coding errors and garbled characters. But sometimes we will find that with this sentence or not, regardless of xxx is the kind of, the browser is always a kind of coding, this situation I will talk about later.

Note that the HTML information is only a declaration that the server has uploaded HTML information to the browser.

2) header ("content-type:text/html; Charset=xxx ");

The function of the header () is to send the information inside the parentheses to the HTTP header. If the contents of the parentheses are the same as in the text, the function and the label are basically the same, and the characters are similar to the first one. But the difference is that if you have this function, the browser will always take the XXX code you require, absolutely will not be disobedient, so this function is very useful. Why is that? Then you have to talk about the difference between HTTP headers and HTML information:

An HTTP header is a string that the server sends HTML messages to the browser before the HTTP protocol. And the label is the HTML information, so the header () sent content first to reach the browser, popular point is the header () priority is higher than (I do not know can be said). If a PHP page has both a header ("content-type:text/html;charset=xxx"), and the browser will only recognize the HTTP header instead of Meta. Of course, this function can only be used within PHP pages.

There is also the question of why the former is absolutely working, and the latter sometimes not. That's why we're going to talk about Apache next.

3) Adddefaultcharset

Apache root directory in the Conf folder, there is the entire Apache configuration document HTTPD.CONF.

With a text editor open httpd.conf, line No. 708 (different versions may be different) has adddefaultcharset xxx,xxx as the encoded name. This line of code means: Set the entire server within the page file HTTP header of the character set for your default XXX character set. With this line, it is equivalent to adding a row header to each file ("content-type:text/html; Charset=xxx "). This will understand why the Utf-8 is clearly set, but the browser always uses the gb2312 reason.

If there is a header in the page ("content-type:text/html; Charset=xxx "), the default character set is changed to the character set you are setting, so this function is always useful. If you put Adddefaultcharset xxx in front of a "#", comment out this sentence, and the page does not contain the header ("Content-type ..."), then this time the META tag has worked.

These are listed below in order of precedence:

.. Header ("content-type:text/html; Charset=xxx ")

.. Adddefaultcharset xxx

..

If you are a web programmer, it is recommended to add a header ("content-type:text/html;charset=xxx") to each of your pages, so that it can be displayed correctly on any server and is more portable.

4) Default_charset configuration in php.ini:

Default_charset = "gb2312" in php.ini defines the default language character set for PHP. Generally recommended to comment out this line, so that the browser according to the charset in the header of the page to automatically select the language instead of making a mandatory rule, so that you can provide multiple languages on the same server Web services.

The application of MB_SUBSTR function in the solution of PHP intercepting Chinese character garbled problem
MySQL Chinese garbled solution collection

http://www.bkjia.com/PHPjc/321816.html www.bkjia.com true http://www.bkjia.com/PHPjc/321816.html techarticle 1) Use the label to set the page encoding the role of this tag is to declare the client's browser with what character set encoding to display the page, XXX can be GB2312, GBK, UTF-8 (and MySQL different, ... )

  • 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.