外部サーバーから HTML をロードしています。HTML マークアップには UTF-8 エンコーディングがあり、ľ、š、č、ť、ž などの文字が含まれています。次のように file_get_contents() を使用して HTML をロードすると:
$html = file_get_contents('http://example.com/foreign.html');
UTF-8 文字を台無しにし、適切な UTF-8 文字の代わりに Å、¾、¤ などのナンセンスを読み込みます。
どうすればこれを解決できますか?
アップデート:
HTMLをファイルに保存し、UTF-8エンコーディングで出力しようとしました。どちらも機能しないため、 file_get_contents() が既に壊れた HTML を返していることを意味します。
更新 2:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sk" lang="sk">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Language" content="sk" />
<title>Test</title>
</head>
<body>
<?php
$html = file_get_contents('http://example.com');
echo htmlentities($html);
?>
</body>
</html>