douhe4336 2014-12-24 19:10
浏览 168
已采纳

从HTML文本中解析并提取数据

I have the below HTML text stored in a variable $domText

<TR class="tableclass">
  <TD>Veteran Job Information</TD>
  <TD>9.00</TD>
  <TD>1.2</TD>
  <TD><INPUT type = "text" name = "notes"></TD>
</TR>

I want to check if the Text content in the First (Here "Veteran Job...") is equal to "Benefit Job" then need to store the values in second and third (Here 9.00 and 1.2) tags to a PHP Variable.

Below is what I have tried to do, But I am getting errors and my code is not working at all. "Invalid argument supplied for foreach()"

        $dom_ChangeResults = new DOMDocument();
        $dom_ChangeResults->loadHTML($domText); //Load the current changes as HTML String
        $dom_TableTags = $dom_ChangeResults->getElementsByTagName("TR"); //Check table data tags for Full time to PartTime Change
        $rows = $dom_TableTags->item(0)->getElementsByTagName('TD');

        /*** loop over the table rows ***/
        foreach ($rows as $row)
        {
            /*** get each column by tag name ***/
            $cols = $row->getElementsByTagName('td');
            /*** echo the values ***/
            echo $cols->item(0)->nodeValue.'<br />';
            echo $cols->item(1)->nodeValue.'<br />';
            echo $cols->item(2)->nodeValue;
            echo '<hr />';
        }
  • 写回答

2条回答 默认 最新

  • duanliaozhi2915 2014-12-24 19:28
    关注

    Don't use capitalized tags with PHP DOMElement. Don't know if this is all code that You gave us, but capitalization in PHP script is main issue, getElementsByTagName('TD') will return empty list, where getElementsByTagName('td') will return populated list.

        $dom_TableTags = $dom_ChangeResults->getElementsByTagName("TR"); //Check table data tags for Full time to PartTime Change
        $rows = $dom_TableTags->item(0)->getElementsByTagName('TD');
    

    shouldn't this be (or You have just one row?):

        $dom_TableTags = $dom_ChangeResults->getElementsByTagName("table"); //Check table data tags for Full time to PartTime Change
        $rows = $dom_TableTags->item(0)->getElementsByTagName('tr');
    

    well here is working sample code

    $domText = <<<DOM
            <TABLE>
            <TR class="tableclass">
              <TD>Veteran Job Information</TD>
              <TD>9.00</TD>
              <TD>1.2</TD>
              <TD><INPUT type = "text" name = "notes"></TD>
            </TR>
            <TR class="tableclass">
              <TD>Veteran Job Information</TD>
              <TD>9.00</TD>
              <TD>1.2</TD>
              <TD><INPUT type = "text" name = "notes"></TD>
            </TR>
            <TR class="tableclass">
              <TD>Veteran Job Information</TD>
              <TD>9.00</TD>
              <TD>1.2</TD>
              <TD><INPUT type = "text" name = "notes"></TD>
            </TR>
            </TABLE>
    DOM;
    
        $dom_ChangeResults = new DOMDocument();
        $dom_ChangeResults->loadHTML($domText); //Load the current changes as HTML String
        $dom_TableTags = $dom_ChangeResults->getElementsByTagName("table"); //Check table data tags for Full time to PartTime Change
        $rows = $dom_TableTags->item(0)->getElementsByTagName('tr');
    
        /*** loop over the table rows ***/
        foreach ($rows as $row)
        {
                /*** get each column by tag name ***/
                $cols = $row->getElementsByTagName('td');
                /*** echo the values ***/
                echo $cols->item(0)->nodeValue.'<br />';
                echo $cols->item(1)->nodeValue.'<br />';
                echo $cols->item(2)->nodeValue;
                echo '<hr />';
        }
    

    EDIT

    To work with data with one <tr> element:

    $dom_ChangeResults = new DOMDocument();
    $dom_ChangeResults->loadHTML($domText); //Load the current changes as HTML String
    //$dom_TableTags = $dom_ChangeResults->getElementsByTagName("tr"); //Check table data tags for Full time to PartTime Change
    $rows = $dom_ChangeResults->getElementsByTagName('tr');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘