我想從許多HTML文件中讀取並顯示標題(包含在h1標記中)的內容。這些文件都在同一個文件夾中。閱讀和編碼html
這是HTML文件的樣子:
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'>
<html>
<head>
<title>A title</title>
<style type='text/css'>
... Styles here ...
</style>
</head>
<body>
<h1>Être aidant</h1>
<p>En général, les aidants doivent équilibrer...</p>
... more tags ...
</body>
我試圖用這個PHP腳本來顯示從H1標籤的內容:
<?php
foreach (glob("test/*.html") as $file) {
$file_handle = fopen($file, "r");
$doc = new DOMDocument();
$doc->loadHTMLfile($file);
$title = $doc->getElementsByTagName('h1');
if ($title && 0<$title->length) {
$title = $title->item(0);
$content = $doc->savehtml($title);
echo $content;
}
fclose($file_handle);
}
?>
但輸出包含錯誤的字符。對於示例文件,輸出爲:
Être aidant
如何實現此輸出?
Être aidant
文件是否保存爲UTF8?您是否嘗試將字符串轉換爲https://secure.php.net/manual/en/function.fopen.php#104325 – x29a