1. 程式人生 > >php 抓取頁面亂碼

php 抓取頁面亂碼

在抓取頁面的時候出現類似�������這樣亂碼解決方法如下

1、轉換編碼

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

2、資料經過gzip壓縮

curl獲取資料的時候設定新增下面的選項:
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
使用file_get_contents函式需要安裝zlib庫
$data = file_get_contents("compress.zlib://".$url);

3、獲取資料後顯示亂碼

在頂部增加下面的程式碼
header("Content-type: text/html; charset=utf-8"
)
;