On the conversion method of PHP character encoding

Source: Internet
Author: User
    1. Print_r (iconv_get_encoding ("All"));
Copy Code

2, encode the string to convert:

    1. echo iconv (' Utf-8 ', ' gb2312 ', ' we '); Convert ' us ' from UTF8 encoding to gb2312 encoding
    2. **iconv (In_charset,outcharset//translit//ignore, $string);//' translit ': if there are no characters in the encoded format of the output, you can find the substitution in similar encodings; ' IGNORE ': If the output format encoding does not contain a character in a string, the encoding of subsequent characters can be skipped. Otherwise, the output is interrupted at transcoding failure, resulting in an error.
Copy Code

3, the string encoding conversion (can automatically determine the encoding type, but heard that efficiency is not iconv good. )

    1. Mb_convert_encoding (' we ', ' utf-8 ', ' gb2312 '); Convert ' us ' from gb2312 to UTF8
    2. Mb_convert_encoding (' we ', ' utf-8 '); Convert ' us ' to UTF8 encoded format
    3. $str = mb_convert_encoding ($str, "EUC-JP", "Auto");
    4. $str = mb_convert_encoding ($str, "Ucs-2le", "JIS, Eucjp-win, Sjis-win");
    5. * * The third parameter can also be an array () Form
Copy Code

When it comes to the use of mb_convert_encoding encoding conversion functions, you can read:

    • PHP Code conversion function mb_convert_encoding and Iconv
    • PHP Code conversion function mb_convert_encoding and iconv usage instructions

4. Set the encoding format:

    1. Iconv_set_encoding ("internal_encoding", "UTF-8"); Set internal encoding to UTF8
    2. Iconv_set_encoding ("output_encoding", "iso-8859-1"); Set output encoding to Iso-8859-1
    3. Setting options: 1.input_encoding 2.output_encoding 3.internal_encoding
    4. **ISO-8859-1 encoding is single byte encoded, backwards compatible with ASCII, Latin1 is iso-8859-1 alias
Copy Code

5. View string Encoding: Format: String mb_detect_encoding (string$str[,mixed$encoding_list= mb_detect_order () [, bool$strict= false]] )

    1. $STR = ' encoding ';
    2. echo mb_detect_encoding ($STR); : UTF-8
    3. Echo mb_detect_encoding ($str, "Auto");
    4. Echo mb_detect_encoding ($str, "JIS, Eucjp-win, Sjis-win");
    5. $ary [] = "ASCII";
    6. $ary [] = "JIS";
    7. $ary [] = "EUC-JP";
    8. Echo mb_detect_encoding ($str, $ary);
Copy Code

6. View the file encoding method:

    1. $file = ' text3.txt ';
    2. Echo getfileencoding (file_get_contents ($file)); Output Utf-16le
Copy Code

7, determines whether the string conforms to the specified format encoding: format: BOOL Mb_check_encoding ([string$var=null[,string$encoding= mb_internal_encoding ()])

    1. $string = "\X00\X81";
    2. $encoding = "Shift_JIS";
    3. Mb_check_encoding ($string, $encoding)//:true
Copy Code

8, character encoding conversion for single or multiple variables: format: stringmb_convert_variables (string$to_encoding,mixed$from_encoding,mixed& $vars [, Mixed &$ ...] * * * $from _encoding: Can be in the form of numbers, separated by commas in string or struct form.

    1. $str 1 = ' test code '; $str 21 = ' Test Code 2 ';
    2. $inputenc = Mb_convert_variables ("UTF-8", "utf-8,gbk,gb2312", $str 1, $str 2);
    3. Var_dump ($INPUTENC); : String (5) "UTF-8"
    4. Var_dump ($str 1); : String (12) "Test Code"
    5. **cp936 is GBK.
Copy Code
  • 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.