Php _ Character encoding

Source: Internet
Author: User
If the output encoding format does not contain any character, you can search for the replacement character in the similar encoding; 'ignore': If the output encoding format does not contain a character in the string, it can skip encoding of subsequent characters. Otherwise, output will be interrupted when transcoding fails and an error will be generated.

1. view the input, internal, and output code on the page:

Print_r (iconv_get_encoding ("all "));

2. encode and convert the specified string:

Echo iconv ('utf-8', 'gb2312', 'Ours '); // Convert 'Ours from utf8 to gb2312 encoding.

** Iconv (in_charset, outcharset // Transcoder // IGNORE, $ string); // 'transtransencoding': If the output encoding format does not contain any character, you can search for replacement in the similar encoding; 'ignore': If the output format encoding does not contain a character in the string, you can skip the subsequent encoding. Otherwise, output will be interrupted when transcoding fails and an error will be generated.

3. encode and convert strings (the encoding type can be determined automatically, but I heard the efficiency is not as good as iconv .)

Mb_convert_encoding ('Ours ', 'utf-8', 'gb2312'); // Convert 'Ours from gb2312 to utf8

Mb_convert_encoding ('Ours ', 'utf-8'); // Convert 'Ours' to utf8 encoding format

/* "Auto" is expanded to "ASCII, JIS, UTF-8, EUC-JP, SJIS "*/
$ Str = mb_convert_encoding ($ str, "EUC-JP", "auto ");

/* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */
$ Str = mb_convert_encoding ($ str, "UCS-2LE", "JIS, eucjp-win, sjis-win ");

** The third parameter can also be in the array () format.

4. set the encoding format:

Iconv_set_encoding ("internal_encoding", "UTF-8"); // set the internal encoding to utf8
Iconv_set_encoding ("output_encoding", "ISO-8859-1"); // sets the output encoding to a ISO-8859-1

Setting options: 1. input_encoding 2. output_encoding 3. internal_encoding

** ISO-8859-1 encoding is single-byte encoding, downward compatible with ASCII, Latin1 is an alias for the ISO-8859-1

5. view the string encoding method:

Format: string mb_detect_encoding (string $ str [, mixed $ encoding_list = mb_detect_order () [, bool $ strict = false])

$ Str = 'Encoding method ';

Echo mb_detect_encoding ($ str); // UTF-8

/* "Auto" is expanded to "ASCII, JIS, UTF-8, EUC-JP, SJIS "*/
Echo mb_detect_encoding ($ str, "auto ");

/* Specify encoding_list character encoding by comma separated list */
Echo mb_detect_encoding ($ str, "JIS, eucjp-win, sjis-win ");

/* Use array to specify encoding_list */
$ Ary [] = "ASCII ";
$ Ary [] = "JIS ";
$ Ary [] = "EUC-JP ";
Echo mb_detect_encoding ($ str, $ ary );

6. view the file encoding method:

$ File = 'text3.txt ';
Echo getFileEncoding (file_get_contents ($ file); // output UTF-16LE

7. check whether the string complies with the specified format encoding:

Format: bool mb_check_encoding ([string $ var = NULL [, string $ encoding = mb_internal_encoding ()])

$ String = "\ x00 \ x81 ";
$ Encoding = "Shift_JIS ";

Mb_check_encoding ($ string, $ encoding) //: true

8. character encoding conversion for one or more variables:

Format: stringmb_convert_variables (string $ to_encoding, mixed $ from_encoding, mixed & $ vars [, mixed & $...])

** $ From_encoding: it can be a number, separated by commas.

$ Str1 = 'Test code'; $ str21 = 'Test code2 ';

$ Inputenc = mb_convert_variables ("UTF-8", "UTF-8, GBK, GB2312", $ str1, $ str2 );

Var_dump ($ inputenc); //: string (5) "UTF-8"
Var_dump ($ str1); //: string (12) "test code"


** CP936 is GBK

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.