Home > Backend Development > PHP Tutorial > php character encoding conversion

php character encoding conversion

不言
Release: 2023-03-22 16:28:01
Original
18828 people have browsed it

This article mainly introduces the method of PHP character encoding conversion. The steps of the method are also relatively detailed. I hope it can help you.

iconv — Convert string to requested character encoding(PHP 4 >= 4.0.5, PHP 5)

mb_convert_encoding — Convert character encoding(PHP 4 >= 4.0.6, PHP 5 )

Usage:
string mb_convert_encoding (string str, string to_encoding [, mixed from_encoding] )
You need to enable the mbstring extension library first, and set in php.ini; extension=php_mbstring.dll in front of ; Remove


string iconv (string in_charset, string out_charset, string str)
Note:
In addition to specifying the encoding to be converted to, the second parameter can also add two A suffix: //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 directly converted Characters to convert, and the default effect is to truncate from the first illegal character.
Returns the converted string or FALSE on failure.

Use:
1. It is found that iconv will make an error when converting the character "-" to gb2312. If there is no ignore parameter, all characters after this character None of the strings can be saved. No matter what, these

"-" 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 according to 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 order of "ASCII,JIS,EUC-JP,SJIS,UTF-8" also has different effects

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

.
from_encoding is specified by character code name before conversion. it can be array or string - comma separated

enumerated list. If it is not specified, the internal encoding will be used.

$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");


Conclusion

1. mb_convert_encoding() This function will automatically identify the encoding based on the content, but the execution efficiency is worse than iconv;
2. Then there is a saying that iconv() will be abnormal when converting some characters. There is a saying on the Internet: it is found that iconv will make an error when converting the character "-" to gb2312. If there is no ignore parameter, all the characters after this character The string 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.
3. mb_convert_encoding() is a PHP extension function and can only be used when the extension library is enabled; iconv It is a built-in function in PHP and can be used without opening an additional extension library.
4. Under normal circumstances, iconv is used. Only when the original encoding cannot be determined, or iconv cannot be displayed normally after conversion, use the mb_convert_encoding function

Related recommendations:

How to solve php encoding conversion garbled code

php array encoding conversion example

php encoding conversion analysis

The above is the detailed content of php character encoding conversion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template