Home  >  Article  >  Backend Development  >  How to convert utf8 and gbk encoding in php

How to convert utf8 and gbk encoding in php

PHPz
PHPzOriginal
2023-04-12 19:57:362003browse

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

  1. Use the iconv function to convert

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;
  1. Use the mb_convert_encoding function to convert

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

  1. Use the iconv function to convert

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;
  1. Use the mb_convert_encoding function to convert

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

  1. Must be sure when performing the conversion The character set to be converted and the target character set, otherwise garbled characters will appear.
  2. When using the iconv function for conversion, the character set names must be strictly corresponding, and some cumbersome situations may occur. You can use the mb_convert_encoding function as an alternative.
  3. If garbled characters appear when performing conversion, you should first check whether the original character set of the string to be converted is correct, and whether the name of the character set is correctly specified when using the iconv function.

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!

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