如何使用dom php解析器 [英] how to use dom php parser

查看:70
本文介绍了如何使用dom php解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在PHP中解析DOM:

我有一个我想要解析的HTML文件。它有一堆这样的DIV:

 < div id =interestingbox> 
< div id =interestingdetailsclass =txtnormal>
< div> Content1< / div>
< div> Content2< / div>
< / div>
< / div>

< div id =interestingbox>
...

我正在尝试获取许多内容div框使用php。
如何使用DOM解析器来执行此操作?



谢谢!

解决方案首先我要告诉你,你不能在两个不同的div上使用相同的id;那里有类。每个元素都应该有唯一的ID。



使用id =interestingbox获取div的内容的代码

  $ html ='
< html>
< head>< / head>
< body>
< div id =interestingbox>
< div id =interestingdetailsclass =txtnormal>
< div> Content1< / div>
< div> Content2< / div>
< / div>
< / div>

< div id =interestingbox2>< a href =#>链接< / a>< / div>
< / body>
< / html>';


$ dom_document = new DOMDocument();

$ dom_document-> loadHTML($ html);

//使用DOMXpath使用DOM导航html
$ dom_xpath = new DOMXpath($ dom_document);

//如果你想得到的div与id = funbox
$ elements = $ dom_xpath-> query(* / div [@ id ='interestingbox']);

if(!is_null($ elements)){

foreach($ elements as $ element){
echo\\\
[。 $元素 - >节点名称。 ];

$ nodes = $ element-> childNodes;
foreach($ nodes as $ node){
echo $ node-> nodeValue。 \\\
;
}

}
}

// OUTPUT
[div] {
Content1
Content2
}

课程示例:

  $ html ='
< html>
< head>< / head>
< body>
< div class =interestingbox>
< div id =interestingdetailsclass =txtnormal>
< div> Content1< / div>
< div> Content2< / div>
< / div>
< / div>

< div class =interestingbox>< a href =#>链接< / a>< / div>
< / body>
< / html>';

//与以前一样..只是更改xpath

[...]

$ elements = $ dom_xpath->查询( * /格[@类= 'interestingbox']);

[...]

// OUTPUT
[div] {
Content1
Content2
}

[div] {
a link
}

参考 DOMXPath 页面了解更多详情。


I'm new to DOM parsing in PHP:
I have a HTML file that I'm trying to parse. It has a bunch of DIVs like this:

<div id="interestingbox"> 
   <div id="interestingdetails" class="txtnormal">
        <div>Content1</div>
        <div>Content2</div>
   </div>
</div>

<div id="interestingbox"> 
......

I'm trying to get the contents of the many div boxes using php. How can I use the DOM parser to do this?

Thanks!

解决方案

First i have to tell you that you can't use the same id on two different divs; there are classes for that point. Every element should have an unique id.

Code to get the contents of the div with id="interestingbox"

$html = '
<html>
<head></head>
<body>
<div id="interestingbox"> 
   <div id="interestingdetails" class="txtnormal">
        <div>Content1</div>
        <div>Content2</div>
   </div>
</div>

<div id="interestingbox2"><a href="#">a link</a></div>
</body>
</html>';


$dom_document = new DOMDocument();

$dom_document->loadHTML($html);

//use DOMXpath to navigate the html with the DOM
$dom_xpath = new DOMXpath($dom_document);

// if you want to get the div with id=interestingbox
$elements = $dom_xpath->query("*/div[@id='interestingbox']");

if (!is_null($elements)) {

  foreach ($elements as $element) {
    echo "\n[". $element->nodeName. "]";

    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
      echo $node->nodeValue. "\n";
    }

  }
}

//OUTPUT
[div]  {
        Content1
        Content2
}

Example with classes:

$html = '
<html>
<head></head>
<body>
<div class="interestingbox"> 
   <div id="interestingdetails" class="txtnormal">
        <div>Content1</div>
        <div>Content2</div>
   </div>
</div>

<div class="interestingbox"><a href="#">a link</a></div>
</body>
</html>';

//the same as before.. just change the xpath

[...]

$elements = $dom_xpath->query("*/div[@class='interestingbox']");

[...]

//OUTPUT
[div]  {
        Content1
        Content2
}

[div]  {
a link
}

Refer to the DOMXPath page for more details.

这篇关于如何使用dom php解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆