简单的HTML解析器 - Foreach返回14行而不是7行

时间:2015-01-11 19:01:02

标签: php simple-html-dom

我有像这样的HTML内容......

  require('simple-parser.php');    
$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;

为每个循环生成一个json

$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;

Json Response是

{
"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":""
}
]
}

问题在于foreach。如何跳过tr内的td。我在表中有7行,ID为“specialTbl”。但是for each在json中返回14行,因为它循环遍历名为runsOn的表。

如何避免在td(4th)内循环遍历表?

1 个答案:

答案 0 :(得分:1)

您必须在array_push循环外使用foreach

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);