Php string encoding problems

Source: Internet
Author: User
Php string encoding problem 1. PHP encoding conversion function mb_convert_encoding-Convertcharacterencoding (PHP4 & gt; 4.0.6, PHP5) usage: Compile (stringstr, stringto_encoding [, mixedfrom_e php string encoding problem
I. PHP code conversion functions
Mb_convert_encoding-Convert character encoding (PHP 4> = 4.0.6, PHP 5)
Usage:
String mb_convert_encoding (string str, string to_encoding [, mixed from_encoding])
Enable the mbstring extension Library first. in php. ini, remove the extension before php_mbstring.dll.

String iconv (string in_charset, string out_charset, string str)
Note:
The second parameter, in addition to specifying the encoding to be converted, can also add two suffixes: // transcoder and // IGNORE,
Where:
// Transcoder automatically converts a character that cannot be directly converted into one or more similar characters,
// IGNORE ignores the characters that cannot be converted. by default, it is truncated from the first invalid character.
Returns the converted string or FALSE on failure.


II. automatically obtain string encoding functions
When using the mb_detect_encoding function in php for encoding recognition, many people have encountered the problem of incorrect recognition encoding, such, or UTF-8 and GBK (here mainly for the cp936 judgment), said online due to short character is, mb_detect_encoding will be wrong.
For example:

$ Encode = mb_detect_encoding ($ keytitle, array ("ASCII", 'utf-8', "gb2312'," GBK ", 'big5 ′));
If ($ encode = "UTF-8 ″){
$ Keytitle = iconv ("UTF-8", "GBK", $ keytitle );
}

The purpose of this code is to check whether the encoding of the string is UTF-8, if yes, it is converted to GBK.

Here, mb refers to multi-bytes, which is an abbreviation of Qiniu B.

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.