2008-09-17 146 views
5

我正在使用一個小的模板引擎,並且我正在使用DOMDocument來解析頁面。我的測試頁,到目前爲止是這樣的:PHP DOMDocument剝離HTML標籤

<block name="content"> 

    <?php echo 'this is some rendered PHP! <br />' ?> 

    <p>Main column of <span>content</span></p> 

</block> 

而且我班的部分看起來像這樣:

private function parse($tag, $attr = 'name') 
{ 
    $strict = 0; 
    /*** the array to return ***/ 
    $out = array(); 
    if($this->totalBlocks() > 0) 
    { 
     /*** a new dom object ***/ 
     $dom = new domDocument; 
     /*** discard white space ***/ 
     $dom->preserveWhiteSpace = false; 

     /*** load the html into the object ***/ 
     if($strict==1) 
     { 
      $dom->loadXML($this->file_contents); 
     } 
     else 
     { 
      $dom->loadHTML($this->file_contents); 
     } 

     /*** the tag by its tag name ***/ 
     $content = $dom->getElementsByTagname($tag); 

     $i = 0; 
     foreach ($content as $item) 
     { 
      /*** add node value to the out array ***/ 
      $out[$i]['name'] = $item->getAttribute($attr); 
      $out[$i]['value'] = $item->nodeValue; 
      $i++; 
     } 
    } 

    return $out; 
} 

我有工作,我想的方式,它抓住每一個<塊>上頁面並注入它的內容到我的模板,但是,它剝離<塊>中的HTML標籤,從而返回不<p>或<跨度>標籤以下內容:

this is some rendered PHP! Main column of content 

我在這裏做錯了什麼? :)謝謝

回答

9

Nothing:nodeValue是樹的值部分的連接,並且永遠不會有標記。

我會做些什麼,使在$節點樹的HTML片段是這樣的:


$doc = new DOMDocument(); 
foreach($node->childNodes as $child) { 
    $doc->appendChild($doc->importNode($child, true)); 
} 
return $doc->saveHTML(); 

HTML「片段」其實比你想象的更首次有問題的,因爲他們往往缺乏諸如文檔類型和字符集之類的東西,這使得很難確定性地在DOM樹和HTML片段的部分之間來回切換。