一旦我在foreach中完成了節點轉換,如何獲得$ myContent的副本,並完成所有替換的節點?如何編寫xpath轉換的結果
回聲$ XPath不工作
$myContent ="<h1>This word should not be replaced: test</h1>.
But this one should be replaced: Test";
@$dom = new DOMDocument;
@$dom->loadHTML($myContent);
$xPath = new DOMXPath(@$dom);
foreach($xPath->query("//text()[contains(.,'test') and not(ancestor::h1)]") as $node)
{
$node->textContent = "<b>".trim($match[1])."</b>";
}
您可能還想停止@錯誤抑制 - 它確實會影響性能,但會使問題難以跟蹤 – 2010-11-05 21:58:11