簡體   English   中英

獲取每個tr的表的td值

[英]Get td value of the table for each tr

如何使用DOM將表中每個tr的內部值提取到td 我有一張這樣的桌子:

<table>
   <tbody>
      <tr class="rowData">
         <td class="cellData">
            <a href="#"><span> DATA 1 </span></a>
         </td>
         <td class="cellData">
            <div class="div1"><div class="div2"> DATA 1 a </div></div>
         </td>
         <td class="cellData">
            <div class="div1"><div class="div2"> DATA 1 b </div></div>
         </td>
         <td class="cellData">
            <div class="div1"><div class="div2"> DATA 1 c </div></div>
         </td>
      </tr>
      <tr class="rowData">
         <td class="cellData">
            <a href="#"><span> DATA 2 </span></a>
         </td>
         <td class="cellData">
            <div class="div1"><div class="div2"> DATA 2 a </div></div>
         </td>
         <td class="cellData">
            <div class="div1"><div class="div2"> DATA 2 b </div></div>
         </td>
         <td class="cellData">
            <div class="div1"><div class="div2"> DATA 2 c </div></div>
         </td>
      </tr>
      <tr class="rowData">
         <td class="cellData">
            <a href="#"><span> DATA 3 </span></a>
         </td>
         <td class="cellData">
            <div class="div1"><div class="div2"> DATA 3 a </div></div>
         </td>
         <td class="cellData">
            <div class="div1"><div class="div2"> DATA 3 b </div></div>
         </td>
         <td class="cellData">
            <div class="div1"><div class="div2"> DATA 3 c </div></div>
         </td>
      </tr>
   </tbody>
<table>

我會得到的是:每行

<label> DATA n </label>
<input value="DATA n a">
<input value="DATA n b">
<input value="DATA n c">

我被這段代碼所困擾:

$html = file_get_contents($link);
$html2 = (preg_replace('/\s+/', ' ', $html));
$doc = new DOMDocument();
$doc->loadHTML($html2);
$xpath = new DOMXPath($doc);
$tables = $doc->getElementsByTagName('table');
foreach($xpath->query('.//tbody/tr[@class="rowData"]') as $node){
}
foreach($xpath->query('.//tbody/tr/td/div/div[@class="div2"]') as $node){
}
foreach($xpath->query('.//tbody/tr/td/a/span') as $node){
echo $node->nodeValue;
}

有人能幫我嗎?

這是一個可能的解決方案,實際上是兩個-但評論說一個太丑陋了。 :)

$html = file_get_contents($link);
$html2 = (preg_replace('/\s+/', ' ', $html));
$doc = new DOMDocument();
$doc->loadHTML($html2);

$elements = $doc->getElementsByTagName('tr');
foreach($elements as $node){

$inputs1=$node->getElementsByTagName('div')->item(1); // 0,2,4...does same
$inputs2=$node->getElementsByTagName('div')->item(3);
$inputs3=$node->getElementsByTagName('div')->item(5);

echo '<label>'. $node->firstChild->nodeValue.  '</label>';
echo '<input value="'. $inputs1->nodeValue.  '">';
echo '<input value="'. $inputs2->nodeValue.  '">';
echo '<input value="'. $inputs3->nodeValue.  '">';

//ugly as hell - but it is working :)

/*echo '<input value="'. $node->firstChild->nextSibling->nextSibling->nodeValue.  '">';


echo '<input value="'. $node->firstChild->nextSibling->nextSibling->nextSibling->nextSibling->nodeValue.  '">';

echo '<input value="'. $node->firstChild->nextSibling->nextSibling->nextSibling->nextSibling->nextSibling->nextSibling->nodeValue.  '">';*/

echo '<br>';
}

我猜代碼是不言自明的。 XPath使用了三遍:查找所有表行,獲取標簽和獲取所有輸入值。

foreach($xpath->query('.//tbody/tr[@class="rowData"]') as $row) {
    echo '<label>'.$xpath->query('td[1]/a/span', $row)->item(0)->textContent."</label>\n";
    foreach($xpath->query('td[position() > 1]/div/div', $row) as $col) {
        echo '<input value="'.trim($col->textContent).'" />'."\n";
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM