使用html dom获取Id的某个div

时间:2013-09-18 17:17:06

标签: php dom domdocument

我正在尝试使用html dom获取某些div的内容

这是我的代码

<?php

    $event_id = $_GET['eventId'];

    //Get the url
    $url = "static/section35.html";
    $html = file_get_contents($url);
    $doc = new DOMDocument(); // create DOMDocument
    libxml_use_internal_errors(true);
    $doc->loadHTML($html); // load HTML you can add $html
    if($html) {
    $divs = $doc->getElementById('e' .$event_id);

    $elements = $doc->getElementsByTagName('tbody');

    $toRemove = array();

    // gather a list of tbodys to remove
    foreach($elements as $el)
      if((strpos($el->nodeValue, 'desktop') !== false) && !in_array($el->parentNode, $toRemove, true))
        $toRemove[] = $el->parentNode;    

            foreach($elements as $el)
      if((strpos($el->nodeValue, 'Recommended') !== false) && !in_array($el->parentNode, $toRemove, true))
        $toRemove[] = $el->parentNode;  

    // remove them
    foreach($toRemove as $tbody)
      $tbody->parentNode->removeChild($tbody);

    echo str_replace(array('style="display: none;','</h3><table','http://www.drakulastream.eu'),array('', '<table',''),$doc->saveHTML());
}
  else {
        echo "<center><h3 style='color:#003366;'>There are no events today</h3></center>";
           }
?>

我希望得到div的html结构是这样的:

<div id="event-pane" class="class1">
   <div id="e170923" class="class2">
   <div id="e170948" class="class2">
   <div id="e170923" class="class2">

我想得到id为e170923的div这个div包含在div事件窗格中

我已经使用上面的代码进行了许多试验,但没有工作,我什么都没得到 所以这有什么问题

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

$url = "static/section35.html";
$html = file_get_contents($url);
$dom = new DOMDocument("1.0", "utf-8");
$dom->loadHTMLFile($html);
$div = $dom->getElementById('e170923');

echo $div->textContent;