-
- print_r(iconv_get_encoding("all"));
Copy the code
2 to convert the encoding of the string:
-
- echo iconv('utf-8','gb2312','we'); //Convert 'us' from utf8 encoding to gb2312 encoding
- **iconv(in_charset,outcharset//TRANSLIT// IGNORE,$string);//'TRANSLIT': If the output encoding format does not contain a character, you can find and replace it in a similar encoding; 'IGNORE': If the output format encoding does not contain a character in the string , can skip encoding of subsequent characters. Otherwise, the output will be interrupted when transcoding fails and an error will occur.
Copy the code
3 to convert the string encoding (the encoding type can be automatically determined, but I heard that the efficiency is not as good as iconv.)
-
- mb_convert_encoding('us', 'utf-8', 'gb2312' ); // Convert 'us' from gb2312 to utf8
- mb_convert_encoding('us', 'utf-8'); // Convert 'us' to utf8 encoding format
- $str = mb_convert_encoding($str, "EUC-JP", "auto");
- $str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win , sjis-win");
- **The third parameter can also be in the form of array()
Copy code
When it comes to the usage of the mb_convert_encoding encoding conversion function, you can read:
- php encoding conversion function mb_convert_encoding and iconv
- Instructions for using php encoding conversion function mb_convert_encoding and iconv
4. Set encoding format:
-
- iconv_set_encoding("internal_encoding", "UTF-8"); //Set the internal encoding to utf8
- iconv_set_encoding("output_encoding", "ISO-8859-1"); //Set the output encoding to ISO- 8859-1
- Setting options: 1.input_encoding 2.output_encoding 3.internal_encoding
- **ISO-8859-1 encoding is a single-byte encoding, backward compatible with ASCII, Latin1 is an alias of ISO-8859-1
Copy code
5, check the string encoding method:
Format: string mb_detect_encoding(string$str[,mixed$encoding_list= mb_detect_order()[,bool$strict= false]] )
-
- $str='encoding';
- echo mb_detect_encoding($str); //: UTF-8
- echo mb_detect_encoding($str, "auto");
- echo mb_detect_encoding($str, "JIS, eucjp-win, sjis-win");
- $ary[] = "ASCII";
- $ary[] = "JIS";
- $ary[] = "EUC-JP";
- echo mb_detect_encoding($str, $ ary);
Copy the code
6 and check the file encoding method:
-
- $file = 'text3.txt';
- echo getFileEncoding(file_get_contents($file)); // Output UTF-16LE
Copy code
7 to determine whether the string conforms to the specified format encoding :
Format: bool mb_check_encoding([string$var=NULL[,string$encoding= mb_internal_encoding()]] )
-
- $string="x00x81";
- $encoding="Shift_JIS";
- mb_check_encoding($string,$encoding) //:true
Copy code
8, single or multiple variables Character encoding conversion:
Format: stringmb_convert_variables(string$to_encoding,mixed$from_encoding,mixed&$vars[,mixed&$...] )
**$from_encoding: can be in numeric form, comma separated string or structure form.
-
- $str1= 'Test encoding';$str21= 'Test encoding 2';
- $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 encoding"
- **CP936 is GBK
Copy code
|