Home  >  Article  >  Backend Development  >  How to change string encoding in php

How to change string encoding in php

藏色散人
藏色散人Original
2021-07-22 10:03:263198browse

How to change string encoding in php: 1. Change the string encoding with the general iconv function; 2. Change the string encoding through mb_convert_encoding.

How to change string encoding in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to change the string encoding in php?

php String encoding conversion

php provides two word conversion encoding functions, one is a general iconv function, and the other is to handle multi-byte encoding Convert function mb_convert_encoding, you need to enable the extension php_mbstring

string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )

Description: Convert character encoding (PHP 4 >= 4.0.6, PHP 5)

You need to enable the mbstring extension library first, in php.ini Remove the ; in front of extension=php_mbstring.dll

string iconv ( string in_charset, string out_charset, string str )

Description: Convert string to requested character encoding (PHP 4 >= 4.0.5, PHP 5)

Note:

The second parameter, in addition to specifying the encoding to be converted to, can also add two suffixes: //TRANSLIT and //IGNORE,

Among them:

TRANSLIT will Automatically convert characters that cannot be directly converted into one or more approximate characters.

IGNORE will ignore characters that cannot be converted, and the default effect is to truncate from the first illegal character.

Usage:

1. It is found that iconv will make an error when converting the character "-" to gb2312. Without the ignore parameter, all strings following this character cannot be saved. No matter what, this "-" cannot be converted successfully and cannot be output. In addition, mb_convert_encoding does not have this bug.

2. mb_convert_encoding can specify multiple input encodings. It will automatically identify it based on the content, but the execution efficiency is much worse than iconv; such as: $str = mb_convert_encoding($str,"euc -jp","ASCII,JIS,EUC-JP,SJIS,UTF-8");The effect will be different depending on the order of "ASCII,JIS,EUC-JP,SJIS,UTF-8"

3. Under normal circumstances, iconv is used. The mb_convert_encoding function is used only when the original encoding cannot be determined, or the iconv conversion cannot be displayed normally.

$str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win"); 
$str = mb_convert_encoding($str, "EUC-JP', "auto");

Example:

$content = iconv("GBK", "UTF-8", $content); 
$content = mb_convert_encoding($content, "UTF-8", "GBK");

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to change string encoding in php. For more information, please follow other related articles on the PHP Chinese website!

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