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

报告相同问题?

悬赏问题

  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统