Home > Backend Development > PHP Problem > What to do if php+fread() is garbled

What to do if php+fread() is garbled

藏色散人
Release: 2023-03-17 19:40:02
Original
1539 people have browsed it

php fread() garbled characters are because the encoding of the output page is inconsistent with the encoding of the read file. The solution: 1. Open the corresponding PHP file; 2. Read the file through the fread function; 3. Through " iconv('gbk', 'utf-8', $data)" method just transcodes the read content.

What to do if php+fread() is garbled

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

What to do if php fread() is garbled ?

In php, files are read directly through the fread function. If the file content encounters Chinese characters, the problem will be garbled. How to solve this problem?

It may be that the encoding of the output page is inconsistent with the encoding of the file being read.

Solution:

Try transcoding the content after reading it out

iconv('gbk', 'utf-8', $data)
Copy after login

Related introduction:

iconv

(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)

iconv — Convert a string 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.

Warning

//TRANSLIT 运行细节高度依赖于系统的 iconv() 实现(参见 ICONV_IMPL)。 据悉,某些系统上的实现会直接忽略 //TRANSLIT,所以转换也有可能失败,out_charset 会是不合格的。
Copy after login

str

The string to convert.

Return value

Returns the converted string, or returns false on failure.

fread()

fread() function reads an open file.

The function will stop running when it reaches the specified length or reaches the end of file (EOF) (whichever comes first).

This function returns the read string, or FALSE if it fails.

Syntax

string fread ( resource $handle , int $length )
Copy after login

Parameters

handle The file system pointer is a resource typically created by fopen().​

length Required. Specifies the maximum number of bytes to read.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What to do if php+fread() is garbled. 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