以下代码显示了如何从文档中提取纯文本内容。
<?php
函数 getTextFromNode($Node, $Text = "") {
if ($Node->tagName == null)
return $Text.$Node->textContent;
$Node = $Node->firstChild;
if ($Node != null)
$Text = getTextFromNode($Node, $Text);
while($Node->nextSibling != null) {
$Text = getTextFromNode($Node->nextSibling, $Text);
$Node = $Node->nextSibling;
}
return $Text;
}
函数 getTextFromDocument($DOMDoc) {
return getTextFromNode($DOMDoc->documentElement);
}
$Doc = new DOMDocument();
$Doc->loadHTMLFile("Test.html");
echo getTextFromDocument($Doc)."\n";
?>