dtq81142
2015-01-11 08:44
浏览 120
已采纳

HTML简单解析器如何在每个循环中跳过td中的tr

I have a html content like this...

$html = <<<EOF
<table  id="specialTbl">
   <tbody>
      <tr>
         <td> row-1-td-1</td>
         <td> row-1-td-2</td>
         <td> row-1-td-3</td>
         <td>
            <table class="runsOn">  // Problem starts here
               <tbody>
                  <tr>
                    <td>row-1-td-4-Child-1</td> 
                    <td>row-1-td-4-Child-2</td>               
                  </tr>               
               </tbody>
            </table>
         </td>
         <td> row-1-td-5</td>
         <td> row-1-td-6</td>
      </tr>
      <tr>
         <td> row-2-td-1</td>
         <td> row-2-td-2</td>
         <td> row-2-td-3</td>
         <td>
            <table class="runsOn">
               <tbody>
                  <tr>
                    <td>row-2-td-4-Child-1</td>
                    <td>row-2-td-4-Child-2</td>               
                  </tr>               
               </tbody>
            </table>
         </td>
         <td> row-2-td-5</td>
         <td> row-2-td-6</td>
      </tr>
      <tr>
         <td> row-3-td-1</td>
         <td> row-3-td-2</td>
         <td> row-3-td-3</td>
         <td>
            <table class="runsOn">
               <tbody>
                  <tr>
                    <td>row-3-td-4-Child-1</td>
                    <td>row-3-td-4-Child-2</td>               
                  </tr>               
               </tbody>
            </table>
         </td>
         <td> row-3-td-5</td>
         <td> row-3-td-6</td>
      </tr>
      <tr>
         <td> row-4-td-1</td>
         <td> row-4-td-2</td>
         <td> row-4-td-3</td>
         <td>
            <table class="runsOn">
               <tbody>
                  <tr>
                    <td>row-4-td-4-Child-1</td>
                    <td>row-4-td-4-Child-2</td>               
                  </tr>               
               </tbody>
            </table>
         </td>
         <td> row-4-td-5</td>
         <td> row-4-td-6</td>
      </tr>
      <tr>
         <td> row-5-td-1</td>
         <td> row-5-td-2</td>
         <td> row-5-td-3</td>
         <td>
            <table class="runsOn">
               <tbody>
                  <tr>
                    <td>row-5-td-4-Child-1</td>
                    <td>row-5-td-4-Child-2</td>               
                  </tr>               
               </tbody>
            </table>
         </td>
         <td> row-5-td-5</td>
         <td> row-5-td-6</td>
      </tr>
      <tr>
         <td> row-6-td-1</td>
         <td> row-6-td-2</td>
         <td> row-6-td-3</td>
         <td>
            <table class="runsOn">
               <tbody>
                  <tr>
                    <td>row-6-td-4-Child-1</td>
                    <td>row-6-td-4-Child-2</td>               
                  </tr>               
               </tbody>
            </table>
         </td>
         <td> row-6-td-5</td>
         <td> row-6-td-6</td>
      </tr>
      <tr>
         <td> row-7-td-1</td>
         <td> row-7-td-2</td>
         <td> row-7-td-3</td>
         <td>
            <table class="runsOn">
               <tbody>
                  <tr>
                    <td>row-7-td-4-Child-1</td>
                    <td>row-7-td-4-Child-2</td>               
                  </tr>               
               </tbody>
            </table>
         </td>
         <td> row-7-td-5</td>
         <td> row-7-td-6</td>
      </tr>  
   </tbody>
</table>
EOF;



$html= str_get_html($html);
$table =$html->find('table#specialTbl',0) ; 
$response["response_code"] = 200;
$response["rows"]   = array();
foreach($table->find('tr') as $key=>$value) {
 $post["td1"]= trim(strip_tags($value->find('td',0)->plaintext));
 $post["td2"]= trim(strip_tags($value->find('td',1)->plaintext));
 $post["td3"]= trim(strip_tags($value->find('td',2)->plaintext));
 $post["td4"]= trim(strip_tags($value->find('td',3)->plaintext)); 
 $post["td5"]= trim(strip_tags($value->find('td',4)->plaintext));
 $post["td6"]= trim(strip_tags($value->find('td',5)->plaintext)); 
  array_push($response["rows"], $post);  
}
$json = json_encode($response);
echo $json_content;

And Json Response is

{
"response_code":200,
"rows":[
{
"td1":"row-1-td-1",
"td2":"row-1-td-2",
"td3":"row-1-td-3",
"td4":"row-1-td-4-Child-1 row-1-td-4-Child-2",
"td5":"row-1-td-4-Child-1",
"td6":"row-1-td-4-Child-2"
},
{
"td1":"row-1-td-4-Child-1",
"td2":"row-1-td-4-Child-2",
"td3":"",
"td4":"",
"td5":"",
"td6":""
},
{
"td1":"row-2-td-1",
"td2":"row-2-td-2",
"td3":"row-2-td-3",
"td4":"row-2-td-4-Child-1 row-2-td-4-Child-2",
"td5":"row-2-td-4-Child-1",
"td6":"row-2-td-4-Child-2"
},
{
"td1":"row-2-td-4-Child-1",
"td2":"row-2-td-4-Child-2",
"td3":"",
"td4":"",
"td5":"",
"td6":""
},
{
"td1":"row-3-td-1",
"td2":"row-3-td-2",
"td3":"row-3-td-3",
"td4":"row-3-td-4-Child-1 row-3-td-4-Child-2",
"td5":"row-3-td-4-Child-1",
"td6":"row-3-td-4-Child-2"
},
{
"td1":"row-3-td-4-Child-1",
"td2":"row-3-td-4-Child-2",
"td3":"",
"td4":"",
"td5":"",
"td6":""
},
{
"td1":"row-4-td-1",
"td2":"row-4-td-2",
"td3":"row-4-td-3",
"td4":"row-4-td-4-Child-1 row-4-td-4-Child-2",
"td5":"row-4-td-4-Child-1",
"td6":"row-4-td-4-Child-2"
},
{
"td1":"row-4-td-4-Child-1",
"td2":"row-4-td-4-Child-2",
"td3":"",
"td4":"",
"td5":"",
"td6":""
},
{
"td1":"row-5-td-1",
"td2":"row-5-td-2",
"td3":"row-5-td-3",
"td4":"row-5-td-4-Child-1 row-5-td-4-Child-2",
"td5":"row-5-td-4-Child-1",
"td6":"row-5-td-4-Child-2"
},
{
"td1":"row-5-td-4-Child-1",
"td2":"row-5-td-4-Child-2",
"td3":"",
"td4":"",
"td5":"",
"td6":""
},
{
"td1":"row-6-td-1",
"td2":"row-6-td-2",
"td3":"row-6-td-3",
"td4":"row-6-td-4-Child-1 row-6-td-4-Child-2",
"td5":"row-6-td-4-Child-1",
"td6":"row-6-td-4-Child-2"
},
{
"td1":"row-6-td-4-Child-1",
"td2":"row-6-td-4-Child-2",
"td3":"",
"td4":"",
"td5":"",
"td6":""
},
{
"td1":"row-7-td-1",
"td2":"row-7-td-2",
"td3":"row-7-td-3",
"td4":"row-7-td-4-Child-1 row-7-td-4-Child-2",
"td5":"row-7-td-4-Child-1",
"td6":"row-7-td-4-Child-2"
},
{
"td1":"row-7-td-4-Child-1",
"td2":"row-7-td-4-Child-2",
"td3":"",
"td4":"",
"td5":"",
"td6":""
}
]
}

Problem is with foreach. How can i skip the tr inside a td. I have 7 rows in table with id "specialTbl". But for each returns 14 rows in json as it loops through table called runsOn.

How can i avoid looping through table inside td(4th)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dty9731 2015-01-13 11:17
    已采纳

    It would be easier to use DomDocument with Xpath as follows. DomDocument already is present in PHP5. It will give you the desired output.

    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $xpath = new DOMXpath($doc);
    $response["response_code"] = 200;
    $response["rows"]   = array();
    $trs = $xpath->query("//table[@id='specialTbl']/tbody/tr"); // all child tr's in all child tbody's in any table that has id 'specialTbl'
    foreach ($trs as $tr) {
        $post = array();
        $tds = $xpath->query("td", $tr); // all child td's in $tr
        foreach ($tds as $key => $td) {
            $post["td" . ++$key] = $td->textContent;
        }
        array_push($response["rows"], $post);
    }
    $json_content = json_encode($response);
    echo $json_content;
    

    But you could also keep using http://simplehtmldom.sourceforge.net/manual.htm and use css-like selectors (untested code, I don't have simplehtmldom):

    $html= str_get_html($html);
    $response["response_code"] = 200;
    $response["rows"]   = array();
    $trs = $html->find("table#specialTbl>tbody>tr");
    foreach ($trs as $tr) {
        $post = array();
        $tds = $tr->children();
        foreach ($tds as $key => $td) {
            $post["td" . ++$key] = $td->innertext;
        }
        array_push($response["rows"], $post);
    }
    $json_content = json_encode($response);
    echo $json_content;
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douhuan7862 2015-01-11 10:53

    You could use PHP's DOM parser, and before searching for the tr items, get it to prune out all the nested tables from the HTML structure:

    // Parse the HTML into a DOM object & find the table by ID
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $table = $doc->getElementById('specialTbl');
    
    // Remove all nested TRs from the DOM table object
    $nested = $table->getElementsByTagName('table');
    foreach ($nested as $element)
    {
        // Remove the TRs from thi nested table
        foreach ($element->getElementsByTagName('tr') as $tr)
            $tr->parentNode->removeChild($tr);
    } 
    
    // Now when we search through TRs, we only get the top level ones
    $rows = $table->getElementsByTagName('tr');
    $response = array();
    foreach ($rows as $row)
    {
        // Collect the values of this row's TDs
        $tds = array();
        foreach ($row->getElementsByTagName('td') as $td)
        {
            $tds[] = trim($td->nodeValue);
        }
    
        // Add this row to the response
        $response['rows'][] = $tds;
    }
    
    // Add extra response details
    $response['response_code'] = 200; // You shouldn't need to explicitly send this
    $json = json_encode($response);
    
    // Output JSON
    header('Content-type: application/json'); // Use the correct MIME type
    echo $json;
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题