在使用file_get_contents函數讀取文件時出現亂碼通常是因為文件編碼不匹配導致的。解決方法有以下幾種:
$context來指定文件編碼,例如:$file_contents = file_get_contents('file.txt', false, stream_context_create([
'http' => [
'header' => 'Content-Type: text/plain; charset=utf-8'
]
]));
iconv函數轉換編碼:可以使用iconv函數將文件內容轉換為指定編碼,例如:$file_contents = file_get_contents('file.txt');
$file_contents = iconv('gbk', 'utf-8', $file_contents);
通過以上方法應該可以解決使用file_get_contents讀取文件出現亂碼的問題。