Home > Article > Backend Development > How to convert utf8 and gbk encoding in php
When developing websites, character encoding conversion is often used. One common conversion is to convert UTF-8 encoding to GBK encoding. This article will introduce how to use PHP to convert between UTF-8 and GBK encoding.
1. The difference between UTF-8 encoding and GBK encoding
UTF-8 encoding and GBK encoding are both common character encoding standards, but there are some differences between them.
UTF-8 encoding is a representation method of the Unicode character set. It uses one to four bytes to represent a character and can represent characters from almost all countries, so it is very convenient to use in cross-language environments.
GBK encoding is a character encoding standard used in the Chinese context. It uses two bytes to represent a character and can represent Traditional Chinese, Simplified Chinese and some other Chinese characters, but it cannot represent non-Chinese characters.
2. Use PHP to convert UTF-8 to GBK
iconv is a built-in conversion character set in PHP The function. You can use iconv to convert UTF-8 strings to GBK strings.
$gbkStr = iconv('UTF-8', 'GBK', $utf8Str);
Among them, $utf8Str is the UTF-8 string that needs to be converted, and $gbkStr is the converted GBK string.
For example, convert a UTF-8 encoded string into a GBK encoded string:
$utf8Str = '这是一段UTF-8编码的字符串'; $gbkStr = iconv('UTF-8', 'GBK', $utf8Str); echo $gbkStr;
mb_convert_encoding It is a function built into PHP that can perform character set conversion. It can convert UTF-8 strings to GBK strings and GBK strings to UTF-8 strings.
$gbkStr = mb_convert_encoding($utf8Str, 'GBK', 'UTF-8');
Among them, $utf8Str is the UTF-8 string that needs to be converted, and $gbkStr is the converted GBK string.
For example, convert a UTF-8 encoded string into a GBK encoded string:
$utf8Str = '这是一段UTF-8编码的字符串'; $gbkStr = mb_convert_encoding($utf8Str, 'GBK', 'UTF-8'); echo $gbkStr;
3. Use PHP to convert GBK to UTF-8
iconv can convert GBK strings into UTF-8 strings.
$utf8Str = iconv('GBK', 'UTF-8', $gbkStr);
Among them, $gbkStr is the GBK string that needs to be converted, and $utf8Str is the converted UTF-8 string.
For example, convert a GBK-encoded string into a UTF-8-encoded string:
$gbkStr = '这是一段GBK编码的字符串'; $utf8Str = iconv('GBK', 'UTF-8', $gbkStr); echo $utf8Str;
mb_convert_encoding GBK strings can be converted into UTF-8 strings.
$utf8Str = mb_convert_encoding($gbkStr, 'UTF-8', 'GBK');
Among them, $gbkStr is the GBK string that needs to be converted, and $utf8Str is the converted UTF-8 string.
For example, convert a GBK-encoded string into a UTF-8-encoded string:
$gbkStr = '这是一段GBK编码的字符串'; $utf8Str = mb_convert_encoding($gbkStr, 'UTF-8', 'GBK'); echo $utf8Str;
4. Notes
Summary
This article introduces how to convert UTF-8 encoding to GBK encoding in PHP, and how to convert GBK encoding to UTF-8 encoding. By using the iconv and mb_convert_encoding functions, we can easily convert character encodings to meet different application scenarios. In daily website development, proficiency in using character encoding conversion is one of the most necessary skills.
The above is the detailed content of How to convert utf8 and gbk encoding in php. For more information, please follow other related articles on the PHP Chinese website!