Windows
Перекодировка текста UTF-8 и WINDOWS-1251
$text = iconv(‘windows-1251//IGNORE’, ‘UTF-8//IGNORE’, $text); | |
echo $text; |
UTF-8 в windows-1251
$text = iconv(‘utf-8//IGNORE’, ‘windows-1251//IGNORE’, $text); | |
echo $text; |
Когда ни что не помогает
$text = iconv(‘utf-8//IGNORE’, ‘cp1252//IGNORE’, $text); | |
$text = iconv(‘cp1251//IGNORE’, ‘utf-8//IGNORE’, $text); | |
echo $text; |
Иногда доходит до бреда, но работает:
$text = iconv(‘utf-8//IGNORE’, ‘windows-1251//IGNORE’, $text); | |
$text = iconv(‘windows-1251//IGNORE’, ‘utf-8//IGNORE’, $text); | |
echo $text; |
File_get_contents / CURL
file_get_contents()
или CURL возвращают иероглифы (ÐлмазнÑе боÑÑ) – причина тут не в кодировке, а в отсутствии BOM-метки.
$text = file_get_contents(‘https://example.com’); | |
$text = «\xEF\xBB\xBF» . $text; | |
echo $text; |
PHP
Ещё бывают случаи, когда file_get_contents() возвращает текст в виде:
�mw�Ƒ0������</�&IkAI��f��j4/{�</�&�h�� ��({�o�����</��:/���</�<g���g��(�=�</��9�Paɭ
Это сжатый текст в GZIP, т.к. функция не отправляет правильные заголовки. Решение проблемы через CURL:
function getcontents($url){ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_ENCODING, ‘gzip’); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
return $output; | |
} | |
echo getcontents(‘https://example.com’); |