php - 找到DOM节点并替换它

时间:2015-08-18 15:18:58

标签: php replace nodes domxpath

我想解析DOM并从数据属性名称中查找特定节点。 然后我想用另一个dom替换它。

我成功找到了DOM节点但是我有一些难以替换它。 这是我的代码:

$content = '<article class="post-35">
            <div class="inner">
                <div class="holder dark">
                    <div class="media"></div>
                    <i class="icon-play"></i>
                    <i class="icon-link"></i>
                </div>
                <span class="item-date">July 3, 2015</span>
                <h2 class="item-title"><a href="">A title</a></h2>
                <span class="post-like" data-post-id="35" >
                    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 64 64"><g><path></path></g></svg>
                    <span class="like-count">2</span>
                </span>
            </div>
        </article>

        <article class="post-36">
            <div class="inner">
                <div class="holder dark">
                    <div class="media"></div>
                    <i class="icon-play"></i>
                    <i class="icon-link"></i>
                </div>
                <span class="item-date">July 3, 2015</span>
                <h2 class="item-title"><a href="">A title</a></h2>
                <span class="post-like" data-post-id="36" >
                    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 64 64"><g><path></path></g></svg>
                    <span class="like-count">5</span>
                </span>
            </div>
        </article>';

$dom = new DOMDocument();
@$dom->loadHTML($content);
$finder = new DOMXPath($dom);
$nodes  = $finder->query('//span[@data-post-id]/@data-post-id');
foreach ($nodes as $node) {
    $like = post_like($node->value); // '<span class="my-class"><svg></svg><span>xx</span>'
    $like = $dom->createElement('span',$like);
    //$node->parentNode->parentNode->appendChild($like); // this works but append plain text not html...
    $node->parentNode->replaceChild($like, $node); // fatal error Uncaught exception 'DOMException' with message 'Not Found Error' 
}

有什么问题?

编辑:

当我打印_r节点时,我得到了这个:

DOMAttr Object
(
    [name] => post-id
    [specified] => 1
    [value] => 35
    [ownerElement] => (object value omitted)
    [schemaTypeInfo] => 
    [nodeName] => post-id
    [nodeValue] => 35
    [nodeType] => 2
    [parentNode] => (object value omitted)
    [childNodes] => (object value omitted)
    [firstChild] => (object value omitted)
    [lastChild] => (object value omitted)
    [previousSibling] => (object value omitted)
    [nextSibling] => (object value omitted)
    [attributes] => 
    [ownerDocument] => (object value omitted)
    [namespaceURI] => 
    [prefix] => 
    [localName] => post-id
    [baseURI] => 
    [textContent] => 35
)

2 个答案:

答案 0 :(得分:0)

我认为您应该使用相同的文档来创建元素:

$content = '<div>.....</div>';
$dom = new DOMDocument();
@$dom->loadHTML($content);
$finder = new DOMXPath($dom);
$nodes  = $finder->query('//span[@data-post-id]/@data-post-id');
foreach ($nodes as $node) {
    $like = post_like($node->value); // return html string (<span>...</span>)
    $like = $dom->createElement('span',$like);
    $node->parentNode->replaceChild($like, $node);
}

答案 1 :(得分:0)

您可以使用DOMDocumentFragment appendXML方法添加&#34;字符串&#34;节点正确地绑定到现有DOM。

foreach ($nodes as $node) {
    $like = post_like($node->value);
    $frag = $dom->createDocumentFragment();
    $frag->appendXML("<span>" . $like . "</span>");
    $node->parentNode->parentNode->appendChild($frag);
}