PHP 根据类名解析 HTML 行

标签 php html-table

如何获取具有特定类名的所有行,例如:

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

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

我使用 cURL 从客户端服务器获取页面。

最佳答案

$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 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7550079/

相关文章:

html - 右对齐表格?

php - Mysql 帮助更新具有其他 ID 的价格的行

php - SQL search 查询匹配数组至少一半的关键字

html - 使用显示 :table-row 在 HTML 表格中自动换行

html - CSS 和 Table/Tr 行大小调整

html - 是否允许在 thead 内使用 td?

javascript - 使用 javascript 在 html 表中生成每件商品的价格

php - 跨域的所有页面都需要 HTTPS

PHP/MYSQL : Storing a list or massive table

php - 具有多个绑定(bind)参数的 MySQL UTF-8 全文搜索