检查每个文本节点是否存在于数据库中,如果存在则将一个类添加到其元素节点。
我尝试过以下方式:
function extractText($node) {
if (XML_TEXT_NODE === $node->nodeType || XML_CDATA_SECTION_NODE === $node->nodeType) {
// am considering that Login is the word that exists in DB
if ($node->nodeValue == "Login"):
$node->setAttribute("class", "translated");
return $node->nodeName;
endif;
} else if (XML_ELEMENT_NODE === $node->nodeType || XML_DOCUMENT_NODE === $node->nodeType
|| XML_DOCUMENT_FRAG_NODE === $node->nodeType) {
if ('script' === $node->nodeName || 'style' === $node->nodeName)
return '';
$text = '';
foreach ($node->childNodes as $childNode) {
$text .= extractText($childNode);
}
return $text;
}
}
$doc = new DomDocument;
$doc->loadHTMLFile('test.html');
//var_dump(extractText($doc->getElementsByTagName('body')->item(0)));
echo extractText($doc->getElementsByTagName('body')->item(0));
但它给出了错误消息。
致命错误:调用未定义的方法 DOMText::setAttribute()