doulu1914 2014-10-20 12:05
浏览 62

XPath选择并回显整行,包括<tr>和<th>

I am parsing some table data using Xpath and need to echo the header row plus any rows that contain certain data. I can successfully select what I want but am not sure of the syntax to output the entire rows including the html tr and th tags.

Here is my code. I am not sure how to simplify the verbose output of the selected lines. I tried the commented out line but it strips the tags.

It is similar to this question: XPath in PHP Removes HTML Tags but I do not understand the answer.

$xpath = new DOMXPath($doc);
$rows = $xpath->query('//table//tr');  // extract rows from html
echo '<table>';
$first = true; 
foreach ($rows as $row) {  // process each row of results
    if ($first) {  // echo header row           
       echo '<tr><td>'.$td->item(0)->textContent.'</td><td>'.$td->item(1)->textContent.'</td><td>'.$td->item(2)->textContent.
            '</td><td>'.$td->item(3)->textContent.'</td><td>'.$td->item(4)->textContent.'</td><td>'.$td->item(5)->textContent.
            '</td><td>'.$td->item(6)->textContent.'</td><td>'.$td->item(7)->textContent.'</td></tr>';
       $first = false;
    }
    else {
    $td = $xpath->query('td', $row); 
        $firstnm = trim(strtok($td->item(1)->textContent," "));
        $mmyyyy = substr($td->item(3)->textContent,2,7);
        if ($firstnm == $firstname && $mmyyyy == $thismth)  
               echo '<tr><td>'.$td->item(0)->textContent.'</td><td>'.$td->item(1)->textContent.'</td><td>'.$td->item(2)->textContent.
                    '</td><td>'.$td->item(3)->textContent.'</td><td>'.$td->item(4)->textContent.'</td><td>'.$td->item(5)->textContent.
                    '</td><td>'.$td->item(6)->textContent.'</td><td>'.$td->item(7)->textContent.'</td></tr>';
// THIS STRIPS THE TAGS  echo $row->nodeValue;
        }

    }
}
echo '</table>';
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?