DOMDocument:replaceChild()的问题

时间:2016-08-24 12:08:30

标签: php domdocument

我试图找到<p>中的第一个<div>

<div class="embed-left">
    <h4>Bookmarks</h4>
    <p>Something goes here.</p>
    <p>Read more...</p>
</div>

我已经完成了。

但是,现在,我需要使用链接替换找到的文本,然后将其分配给<span>,然后再使用$url createElement()方法:

$results_links = $this->data_migration->process_embed_find_links();

$dom = new DOMDocument();

foreach ($results_links as $notes):

    $dom->loadHTML($notes['note']);

    $x = $dom->getElementsByTagName('div')->length;

    // Loop through the <div> elements found in the HTML...
    for ($i = 0; $i < $x; $i++):

        $parentNode = $dom->getElementsByTagName('div')->item($i);

        // Here's a <h4> element.
        $childNodeHeading = $dom->getElementsByTagName('div')->item($i)->childNodes->item(1);

        // If the <h4> element is "Bookmarks"...
        if ( $childNodeHeading->nodeValue == "Bookmarks" ):

            // ... then grab the first <p> element.
            $childNodeTitle = $dom->getElementsByTagName('div')->item($i)->childNodes->item(3);

            // Create the appropriate <p> element.
            $title = $dom->createElement('p', $childNodeTitle->nodeValue);
            echo "<p>" . $title->nodeValue . "</p>";

            // Find the `notes_links.from-asset` rows.
            $results_bookmarks_links = $this->data_migration->process_embed_find_links_bookmarks_links(array(
                'note_id' => $notes['note_id'],
                // Send the first <p> tag in the <div> element.
                'title' => htmlentities($childNodeTitle->nodeValue)
            ));

            // Loop through the data (one row returned, but it's more neat to run it through a foreach() function)...
            foreach ($results_bookmarks_links as $index => $link):

                // Assuming there are values (which there has to be, by virtue of the fact that we found the <div> elements in the first place...
                if ( isset($results_bookmarks_links) && ( count($results_bookmarks_links) > 0 ) ):

                    // Create the <span> element for the link item, according to Sina's design.
                    $span = '<span><a href="#">[#' . $notes['note_id'] . ']</a></span>';

                    **$url = $dom->createElement('span', $span);**

                    **$parentNode->replaceChild(
                        $url,
                        $title
                    );**

                endif;

            endforeach;

        endif;

    endfor;

endforeach;

我没有成功。

我无法找出父元素或replaceChild()方法中使用的正确参数。

如果有帮助的话,我已经为我遇到麻烦的主要部分加了大胆。

1 个答案:

答案 0 :(得分:0)

重要的是用现有的p替换包含子节点的新创建的p

以下是一个示例,使用XPath选择要替换的节点:

<?php

$html = <<<END
<div class="embed-left">
    <h4>Bookmarks</h4>
    <p>Something goes here.</p>
    <p>Read more...</p>
</div>
END;

$doc = new DOMDocument;
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED);

$xpath = new DOMXPath($doc);

$nodes = $xpath->query('//div[h4[text()="Bookmarks"]]/p[1]');

foreach ($nodes as $oldnode) {  
    $note = 'TODO'; // build `$note` somewhere

    $link = $doc->createElement('a');
    $link->setAttribute('href', '#');
    $link->textContent = sprintf('[#%s]', $note); 

    $span = $doc->createElement('span');
    $span->appendChild($link);

    $newnode = $doc->createElement('p');
    $newnode->appendChild($span);

    $oldnode->parentNode->replaceChild($newnode, $oldnode);
}

print $doc->saveHTML($doc->documentElement);