Home > Backend Development > PHP Problem > How to convert php gbk to utf8

How to convert php gbk to utf8

藏色散人
Release: 2023-03-07 16:46:01
Original
3828 people have browsed it

php gbk to utf8 method: first create a PHP sample file; then set the header; finally use the "iconv('GB2312', 'UTF-8', $str);" method to convert the string Just change the encoding from GB2312 to utf8.

How to convert php gbk to utf8

The operating environment of this tutorial: windows10 system, php5.6. This article is applicable to all brands of computers.

Recommended: "PHP Video Tutorial"

Convert gbk to utf8

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

iconv — Strings are converted according to the required character encoding

Description

iconv ( string $in_charset , string $out_charset , string $str ) : string
Copy after login

Convert the string str from in_charset to out_charset.

Parameters

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 caused and FALSE is returned.

Caution

//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 FALSE on failure.

The above is the detailed content of How to convert php gbk to utf8. 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