dongyong8098 2013-02-22 12:02
浏览 125
已采纳

PHP DomDocument解析html

I have the following HTML markup

<div contenteditable="true" class="text"></div>
<div contenteditable="true" class="text"></div>
<div style="display: block;" class="ui-draggable">
    <img class='avatar' src=""/>
    <p style="">
    <img class='pic' src=""/><br>
    <span class='fulltext' style="display:none"></span>
    </p>-<span class='create'></span>
    <a class='permalink' href=""></a>
    </div>
 <div contenteditable="true" class="text"></div>
 <div style="display: block;" class="ui-draggable">
    <img class='avatar' src=""/>
    <p style="">
    <img class='pic' src=""/><br>
    <span class='fulltext' style="display:none"></span>
    </p><span class='create'></span><a class='permalink' href=""></a>
    </div>

The parent div's can be more.In order to parse the information and to insert it in the DB I'm using the following code -

$dom = new DOMDocument();
$dom->loadHTML($xml);
$xpath = new DOMXPath($dom);
$div = $xpath->query('//div');
$i=0;
$q=1;
foreach($div as $book) {
    $attr = $book->getAttribute('class');
    //if div contenteditable
    if($attr == 'text') {
        echo '</br>'.$book->nodeValue."</br>";  
    }

    else {
        $new = new DOMDocument();
        $newxpath = new DOMXPath($new);
        $avatar = $xpath->query("(//img[@class='avatar']/@src)[$q]");

        $picture = $xpath->query("(//p/img[@class='pic']/@src)[$q]");
        $fulltext = $xpath->query("(//p/span[@class='fulltext'])[$q]");
        $permalink = $xpath->query("(//a[@class='permalink'])[$q]");
        echo $permalink->item(0)->nodeValue; //date
        echo $permalink->item(0)->getAttribute('href');
        echo $fulltext->item(0)->nodeValue;
        echo $avatar->item(0)->value;
        echo $picture->item(0)->value;
        $q++;
    }
    $i++;
}

But I think that there's a better way for parsing the html. Is there? Thank you in advance

  • 写回答

2条回答 默认 最新

  • donglusou3335 2013-02-22 12:09
    关注

    Note that DOMXPath::query supports a second param called contextparam. Also you won't need a second DOMDocument and DOMXPath inside the loop. Use:

    $avatar = $xpath->query("img[@class='avatar']/@src", $book);
    

    to get <img src=""> attribute nodes relative to the div nodes. If you follow my advices your example should be fine.


    Here comes a version of your code that follows the above said:

    $dom = new DOMDocument();
    $dom->loadHTML($xml);
    
    $xpath = new DOMXPath($dom);
    $divs = $xpath->query('//div');
    
    foreach($divs as $book) {
        $attr = $book->getAttribute('class');
        if($attr == 'text') {
            echo '</br>'.$book->nodeValue."</br>";  
        } else {
            $avatar = $xpath->query("img[@class='avatar']/@src", $book);
            $picture = $xpath->query("p/img[@class='pic']/@src", $book);
            $fulltext = $xpath->query("p/span[@class='fulltext']", $book);
            $permalink = $xpath->query("a[@class='permalink']", $book);
            echo $permalink->item(0)->nodeValue; //date
            echo $permalink->item(0)->getAttribute('href');
            echo $fulltext->item(0)->nodeValue;
            echo $avatar->item(0)->value;
            echo $picture->item(0)->value;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂