【问题标题】:Is there anyway that I can get the table row with a specific td value with PHP or Simple HTML DOM?无论如何,我可以使用 PHP 或简单的 HTML DOM 获取具有特定 td 值的表行吗?
【发布时间】:2014-01-27 23:24:37
【问题描述】:

假设我有下表:

<table>
    <thead>
        <tr>
            <th>Country<th>
            <th>Currency Name</th>
            <th>Currency Code</th>
    <tbody>
        <tr>
            <td>Austria</td>
            <td>Euro</td>
            <td>EUR</td>
        </tr>
        <tr>
            <td>South Africa</td>
            <td>South African Rand</td>
            <td>ZAR</td>
        </tr>
        <tr>
            <td>Greece</td>
            <td>Euro</td>
            <td>EUR</td>
        </tr>
    </tbody>
</table>

如何获取整个&lt;tr&gt; 和包含&lt;td&gt;ZAR&lt;/td&gt; 的内容?使用简单的 HTML DOM 和 PHP?

【问题讨论】:

    标签: php html html-table simple-html-dom


    【解决方案1】:

    Simple 不够聪明,无法直接执行此操作,但对于大多数体面的库(phpquery?),您可以这样做:

    $doc->find('tr:has(td:contains("ZAR"))')
    

    【讨论】:

      【解决方案2】:
      $string = '...'; // your HTML string
      
      $html = str_get_html($string);
      
      foreach ($html->find('tr') as $tr) {
          $flag = 0;
          foreach ($tr->find('td') as $td) {
              if ($tr->plaintext === 'ZAR') {
                  $flag = 1;
                  break;
              }
          }
          if ($flag === 1) {
              echo $tr."\n";
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-02-07
        • 2019-02-09
        • 2012-06-19
        • 2018-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多