search
HomeBackend DevelopmentPHP ProblemHow to convert data encoding from utf-8 to gbk in php

In PHP, you can use the iconv() function to convert the data encoding from utf-8 to gbk. This function can convert the string according to the required character encoding. The syntax "iconv("UTF-8 ","gbk//TRANSLIT",$str)".

How to convert data encoding from utf-8 to gbk in php

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

In php, you can use iconv () function converts the encoding of data from utf-8 to gbk.

<?php
header("Content-type:text/html;charset=UTF-8");
$str= &#39;你好,这里是utf8转gbk!&#39;;
echo $str;
echo &#39;<br />&#39;;
echo iconv("UTF-8","gbk//TRANSLIT",$str); //将字符串的编码从UTF-8转到GB2312
?>

How to convert data encoding from utf-8 to gbk in php

iconv() function can also convert encoding gbk to utf8

<?php
header("Content-type:text/html;charset=GB2312");
$str= &#39;你好,这里是gbk转utf8!&#39;;
echo $str;
echo &#39;<br />&#39;;
echo iconv("GB2312","UTF-8",$str); //将字符串的编码从UTF-8转到GB2312
?>

Description: iconv() function

iconv() function can convert a string according to the required character encoding.

Syntax:

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

Convert the string str from in_charset to out_charset.

Parameter description:

  • $in_charset: Input character set.

  • $out_charset: Output character set.

    If you add the string //TRANSLIT after out_charset, the transliteration function will be enabled. This means that when a character cannot be represented by the target character set, it can be approximated by one or more similar characters. If you add the string //IGNORE, characters that cannot be expressed in the target character set will be silently discarded. Otherwise, an E_NOTICE is raised and false is returned.

    WARNING

    //TRANSLIT operation details are highly dependent on the system's iconv() implementation (see ICONV_IMPL). It is reported that the implementation on some systems will directly ignore //TRANSLIT, so the conversion may fail and out_charset will be unqualified.

  • #$str: The string to be converted.

Return value: Returns the converted string, or returns false on failure.

Starting from version 5.4.0, false will be returned when the character is illegal, unless //IGNORE is specified in the output character. In previous versions, it would return a partial string.

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to convert data encoding from utf-8 to gbk 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor