Home >Backend Development >PHP Tutorial >A brief discussion on PHP character encoding conversion method

A brief discussion on PHP character encoding conversion method

WBOY
WBOYOriginal
2016-07-25 08:53:50970browse
  1. print_r(iconv_get_encoding("all"));
Copy the code

2 to convert the encoding of the string:

  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 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.)

  1. mb_convert_encoding('us', 'utf-8', 'gb2312' ); // Convert 'us' from gb2312 to utf8
  2. mb_convert_encoding('us', 'utf-8'); // Convert 'us' to utf8 encoding 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 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:

  1. iconv_set_encoding("internal_encoding", "UTF-8"); //Set the internal encoding to utf8
  2. iconv_set_encoding("output_encoding", "ISO-8859-1"); //Set the output encoding to ISO- 8859-1
  3. Setting options: 1.input_encoding 2.output_encoding 3.internal_encoding
  4. **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]] )

  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 the code

6 and check the file encoding method:

  1. $file = 'text3.txt';
  2. 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()]] )

  1. $string="x00x81";
  2. $encoding="Shift_JIS";
  3. 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.

  1. $str1= 'Test encoding';$str21= 'Test encoding 2';
  2. $inputenc = mb_convert_variables("UTF-8", "UTF-8,GBK,GB2312", $str1, $str2 );
  3. var_dump($inputenc); //: string(5) "UTF-8"
  4. var_dump($str1); //: string(12) "Test encoding"
  5. **CP936 is GBK
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn