Home  >  Article  >  Backend Development  >  How to solve the problem of garbled code collection in php

How to solve the problem of garbled code collection in php

藏色散人
藏色散人Original
2020-08-07 10:41:222083browse

Solution to php collecting garbled characters: 1. Use the "str=mbconvertencoding(str, "utf-8", "GBK");" method to convert the encoding; 2. Compress the data through gzip; 3. Add "charset=utf8" code at the top.

How to solve the problem of garbled code collection in php

Recommended: "PHP Video Tutorial"

php crawling page garbled code

The solution to garbled characters similar to ������� appears when crawling the page is as follows

1. Convert encoding

str=mbconvertencoding(str, “utf-8”, “GBK”);

2. Data is compressed by gzip

When curl obtains data, add the following options:

curl_setopt($ch, CURLOPT_ENCODING, 'gzip');

Using the file_get_contents function requires installing the zlib library

$data = file_get_contents("compress.zlib://".$url);

3. After obtaining the data, garbled characters are displayed

at the top Add the following code

header("Content-type: text/html; charset=utf-8");

The above is the detailed content of How to solve the problem of garbled code collection 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