douhe4336 2014-12-24 11:10
浏览 169
已采纳

从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 11: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部