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 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM