Questions about PHP string encoding

Source: Internet
Author: User
PHP string Encoding problem
PHP Code conversion function
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])
Need to enable mbstring extension library, in php.ini; Extension=php_mbstring.dll in front of; Remove

String Iconv (String in_charset, String out_charset, String str)
Attention:
The second parameter, in addition to specifying the encoding to be converted to, can also add two suffixes://translit and//ignore,
which
Translit automatically converts characters that cannot be converted directly into one or more approximate characters,
IGNORE ignores characters that cannot be converted, and the default effect is to truncate from the first illegal character.
Returns the converted string or FALSE on failure.


Second, automatically get the string encoding function
When using the Mb_detect_encoding function in PHP to encode recognition, many people have encountered the problem of identifying the coding error, for example, with GB2312 and UTF-8, or UTF-8 and GBK (here is mainly for cp936 judgment), online said that because the character is short, Mb_detect_encoding will have a miscarriage of error.
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 detect whether the encoding of a string is UTF-8, and then convert to GBK.

where MB refers to the meaning of multi-bytes, is one of its abbreviations, true cow 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.