php - 解析html页面 [英] php - parse html page

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

问题描述

 < DIV> divbox< / DIV> 
< p> para1< / p>
< p> para2< / p>
< p> para3< / p>
< table class =table>< tr>< td>< / td>< / tr>< / table>
< p> para4< / p>
< p> para5< / p>

有人可以告诉我如何解析这个html页面来显示para1,para2和para3吗?并删除所有其他内容。



条件:

i想从第一个 <$获取所有内容 c $ c>< p> 到第一个 < table class =table>
$ b

(第一个表总是有类table)



输出:

 < p> para1< / p> 
< p> para2< / p>
< p> para3< / p>


解决方案

  $ d = new domdocument(); 
libxml_use_internal_errors(true);
$ d-> loadHTML($ file);

foreach($ d-> getElementsByTagName(*)as $ el){
if($ el-> tagName ==p)
echo $ el-> textContent,\\\
;
elseif($ el-> tagName ==table)
break;
}

给出

 
para1
para2
para3


<div>divbox</div>
<p>para1</p>
<p>para2</p>
<p>para3</p>
<table class="table"><tr><td></td></tr></table>
<p>para4</p>
<p>para5</p>

could someone please tell me how i can parse this html page to display ONLY para1, para2 and para3? and remove everything else.

condition:
i want to fetch all the content from the first <p> to the first <table class="table">.

(the first table will always have the class "table")

output:

<p>para1</p>
<p>para2</p>
<p>para3</p>

解决方案

$d = new domdocument();
libxml_use_internal_errors(true);
$d->loadHTML($file);

foreach ($d->getElementsByTagName("*") as $el) {
    if ($el->tagName == "p")
        echo $el->textContent, "\n";
    elseif ($el->tagName == "table")
        break;
}

This gives:

para1
para2
para3

这篇关于php - 解析html页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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