2016-08-24 78 views
2

我試圖找到一個<div>第一<p>DOM文檔:問題的replaceChild()

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

我已經做了。

但是現在,我需要更換的鏈接找到的文字,因爲分配給該<span>之前然後在$urlcreateElement()方法的使用:

$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()方法中使用的正確參數。

如果有幫助,我強化了我遇到的主要問題。

回答

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);