I'm trying to parse a simple HTML containing Chinese characters inside script tag. However, after processing by PHP DomDocument, those are converted to some weird characters.
<?php
$html = <<<EOD
<!DOCTYPE html>
<html>
<head>
<script>
const str = "訂閱最新指南";
</script>
</head>
<body>
</body>
</html>
EOD;
$dom = new DOMDocument();
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
$dom->loadHTML($html);
// Trying different approaches to get correct output
echo $dom->saveHTMl();
echo $dom->saveHTML($dom->documentElement);
echo utf8_decode($dom->saveHTML($dom->documentElement));
echo utf8_decode($dom->saveHTML());
Output:
<!DOCTYPE html>
<html>
<head>
<script>
const str = "訂閱最新指南";
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<script>
const str = "訂閱最新指南";
</script>
</head>
<body>
</body>
</html><html>
<head>
<script>
const str = "訂閱最新指南";
</script>
</head>
<body>
</body>
</html><!DOCTYPE html>
<html>
<head>
<script>
const str = "訂閱最新指南";
</script>
</head>
<body>
</body>
</html>