PHP根据类名解析HTML行 [英] PHP parse HTML rows based on class name

查看:66
本文介绍了PHP根据类名解析HTML行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < tr class =dailyeventtext如何获得具有特定类名称的所有行bgcolor =#cfcfcfvalign =top> 

然后将该行中的每个单元格放入数组中?

我使用cURL将页面从客户端的服务器上取消。



预先感谢。

解决方案

  $ matches = array(); 

$ dom = new DOMDocument;

$ dom-> loadHTML($ html);

foreach($ dom-> getElementsByTagName('tr')as $ tr){
if(!$ tr-> hasAttribute('class')){
继续;


$ class = explode('',$ tr-> getAttribute('class'));

if(in_array('dailyeventtext',$ class)){
$ matches [] = $ tr-> getElementsByTagName('td');
}

}


How can I get all the rows with a specific class name such as:

<tr class="dailyeventtext" bgcolor="#cfcfcf" valign="top">

and then place each cell in that row into an array?

I used cURL to get the page off the client's server.

Thanks in advance.

解决方案

$matches = array();

$dom = new DOMDocument;

$dom->loadHTML($html);

foreach($dom->getElementsByTagName('tr') as $tr) {
    if ( ! $tr->hasAttribute('class')) {
       continue;
    }

    $class = explode(' ', $tr->getAttribute('class'));

    if (in_array('dailyeventtext', $class)) {
       $matches[] = $tr->getElementsByTagName('td');
    }

}

这篇关于PHP根据类名解析HTML行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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