Differences between PHPiconv and mb_convert_encoding PHP often uses the iconv function in character transcoding, but encountered a problem in a certain transcoding. for example: $ str = 'weather today-Clear '; $ str = iconv ('utf-8', 'gbk', $ str,); differences between PHP iconv and mb_convert_encoding after Transcoding
PHP often uses the iconv function in character transcoding, but encountered a problem in a certain transcoding;
Example:
$ Str = 'weather today-Clear ';
$ Str = iconv ('utf-8', 'gbk', $ str ,);
After transcoding, $ str is only 'Weather is fine today '. find the manual and find that two suffixes can be added to the second parameter of iconv,
?
? // Transcoder automatically converts a character that cannot be directly converted into one or more similar characters,
? // IGNORE ignores the characters that cannot be converted. by default, it is truncated from the first invalid character.
?
Usage:
1. it is found that iconv will encounter an error when converting the character "-" to gb2312. if the ignore parameter is not available, all strings after this character cannot be saved. In any case, the "-" cannot be converted successfully or output. In addition, mb_convert_encoding does not have this bug.
2. mb_convert_encoding can specify multiple input encodings, which are automatically identified based on the content, but the execution efficiency is much lower than iconv;
3. generally, iconv is used. the mb_convert_encoding function is used only when the encoding of the original encoding cannot be determined or The iconv cannot be displayed normally after conversion.
Note: To use mb_convert_encoding, you need to enable this extension in php. ini.