Home > Backend Development > PHP Problem > How to convert data encoding from utf-8 to gbk in php

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

青灯夜游
Release: 2023-03-15 12:00:01
Original
3971 people have browsed it

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
?>
Copy after login

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
?>
Copy after login

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
Copy after login

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!

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