0

有 HTML 结构,我需要获取 TD 的每个 TR 文本值

<table class="dataTable all_clients" style="width:100%;" cellpadding="0" cellspacing="0" border="0">
      <tbody>
        <tr>
          <td width="15px"></td>
          <td width="25%">
            <strong>Комиссии за сутки</strong>
          </td>
          <td></td>
        </tr>
        <tr>
          <td colspan="5" style="height:5px" class="line"></td>
        </tr>
        <tr>
          <td></td>
          <td style="width:200px;">
            514596        
          </td>
          <td style="text-align:right;width:70px;" class="redData">
            0.00        
          </td>
          <td class="redData">
            USD        
          </td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td style="width:200px;">
            514597        
          </td>
          <td style="text-align:right;width:70px;" class="redData">
            0.00        
          </td>
          <td class="redData">
            RUR        
          </td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td style="width:200px;">
            514598        
          </td>
          <td style="text-align:right;width:70px;" class="redData">
            0.00        
          </td>
          <td class="redData">
            EUR        
          </td>
          <td></td>
        </tr>
      </tbody>
    </table>

PHP代码:首先我得到所有,之后我需要在TD处获取文本值

<? libxml_use_internal_errors(true);
        $dom = new domDocument('1.0', 'UTF-8');
        $dom->strictErrorChecking=false;
        $dom->recover = true;
        @$dom->loadHTML($data);
        $xpath = new DomXPath($dom);

        $elements = $xpath->query("//table[@class='dataTable all_clients']//tr");
        $result = array();
        $title = array();

        if (!is_null($elements)) {

            foreach ($elements as $element) {

                  $titles = $xpath->query('//strong', $element);       //Get titles
                  $numbers = $xpath->query('//td[1]', $element);       //Get number
                  $numbers_flt = $xpath->query('//td[2]', $element);   //Get number 0.00
                  $symbols = $xpath->query('//td[3]', $element);       //Get Symbol

                  for( $i = 0; $i < count($numbers); $i++) {

                        $result['numbers'][$i] = $numbers->nodeValue;
                        $result['numbers_flt'][$i] = $numbers_flt->nodeValue;
                        $result['symbols'][$i] = $symbols->nodeValue;

                  }

                  foreach ($titles as $value) {
                    $title[] = $value->nodeValue;
                  }
            }
        }

        var_dump($result);
            }?>

所以,我需要格式化数组 $result,但仍然没有找到解决方法

4

0 回答 0