检查具有特定类的 td 节点值是否为空


Check td nodeValue with specific class is empty or not

    $html = '
    <table class="views-table cols-0 table"><tbody>
<tr class="odd promotionRow views-row-first" style="width:100%;padding:1% !important;margin-right:5px;float:left;">
<td class="weeklyPromotionImage" style="width:110px;vertical-align:top;">
            <a href="http://dev-site.youngparents.com.sg/yp-weekly/new-newsletter" class="active" style="color:#7abfb5;text-decoration:none;font-family:Lato, sans-serif;font-size:14px;"></a><a href="http://dev-site.youngparents.com.sg/promotion/win-give-your-kid-new-look" style="color:#7abfb5;text-decoration:none;font-family:Lato, sans-serif;font-size:14px;"><img typeof="foaf:Image" src="http://dev-site.youngparents.com.sg/sites/default/files/styles/side_bar_image_110x110/public/new-look-110x110.png?itok=jbnpIaIX" width="110" height="110" alt="" style="width:100% !important;height:auto !important;" data-pin-nopin="true"></a>          </td>
                  <td class="weeklyPromotionImage">
                      </td>
                  <td class="weeklyPromotionTitle" style="width:70%;padding-left:5px;">
            <div><a href="http://dev-site.youngparents.com.sg/promotion/win-give-your-kid-new-look" style="font-size:10px;text-transform:uppercase;font-family:Lato, sans-serif;color:#79BAB2;text-decoration:none;">WIN! Give your kid a new look</a></div>
<div style="font-size:12px;font-family:Lato, sans-serif;color:#79BAB2;text-decoration:none;">Win it now! For the New Look!!</div>          </td>
              </tr>
<tr class="even promotionRow" style="width:100%;padding:1% !important;margin-right:5px;float:left;">
<td class="weeklyPromotionImage" style="width:110px;vertical-align:top;">
            <a href="http://dev-site.youngparents.com.sg/yp-weekly/new-newsletter" class="active" style="color:#7abfb5;text-decoration:none;font-family:Lato, sans-serif;font-size:14px;"></a><a href="http://dev-site.youngparents.com.sg/promotion/win-60-cash-voucher-amazonia-singapore" style="color:#7abfb5;text-decoration:none;font-family:Lato, sans-serif;font-size:14px;"><img typeof="foaf:Image" src="http://dev-site.youngparents.com.sg/sites/default/files/styles/side_bar_image_110x110/public/110%20X%20110%20Amz.png?itok=31JEV5YU" width="110" height="110" alt="" style="width:100% !important;height:auto !important;" data-pin-nopin="true"></a>          </td>
                  <td class="weeklyPromotionImage" style="width:110px;vertical-align:top;">
                      </td>
                  <td class="weeklyPromotionTitle" style="width:70%;padding-left:5px;">
            <div><a href="http://dev-site.youngparents.com.sg/promotion/win-60-cash-voucher-amazonia-singapore" style="font-size:10px;text-transform:uppercase;font-family:Lato, sans-serif;color:#79BAB2;text-decoration:none;">Win $60 cash voucher from Amazonia Singapore!</a></div>
<div style="font-size:12px;font-family:Lato, sans-serif;color:#79BAB2;text-decoration:none;">Win the Cash Voucher from Amazonia Singapore</div>          </td>
              </tr>
<tr class="odd promotionRow views-row-last" style="width:100%;padding:1% !important;margin-right:5px;float:left;">
<td class="weeklyPromotionImage" style="width:110px;vertical-align:top;">
                      </td>
                  <td class="weeklyPromotionImage" style="width:110px;vertical-align:top;">
            <a href="http://dev-site.youngparents.com.sg/yp-weekly/new-newsletter" class="active" style="color:#7abfb5;text-decoration:none;font-family:Lato, sans-serif;font-size:14px;"></a><a href="http://dev-site.youngparents.com.sg/event/young-parents-primary-1-seminar-2015" style="color:#7abfb5;text-decoration:none;font-family:Lato, sans-serif;font-size:14px;"><img typeof="foaf:Image" src="http://dev-site.youngparents.com.sg/sites/default/files/styles/side_bar_image_110x110/public/yp-event-p1.jpg?itok=Jz90Yhwb" width="110" height="110" alt="" style="width:100% !important;height:auto !important;" data-pin-nopin="true"></a>          </td>
                  <td class="weeklyPromotionTitle" style="width:70%;padding-left:5px;">
            <div><a href="http://dev-site.youngparents.com.sg/event/young-parents-primary-1-seminar-2015" style="font-size:10px;text-transform:uppercase;font-family:Lato, sans-serif;color:#79BAB2;text-decoration:none;">Young Parents Primary 1 Seminar 2015</a></div>
<div style="font-size:12px;font-family:Lato, sans-serif;color:#79BAB2;text-decoration:none;"></div>
<p style="width:100%;margin:0 auto;padding:1% 0; color:#79BAB2;font-size:20px;"><a href="http://dev-site.youngparents.com.sg/event/young-parents-primary-1-seminar-2015" style="color:#7abfb5;text-decoration:none;font-family:Lato, sans-serif;font-size:14px;">Young Parents Primary 1 Seminar 2015</a></p>          </td>
              </tr>
</tbody></table>
';

我想检查类"每周促销图像"的td值是否为空。

我尝试了以下代码。它只是作为空结果返回。但它的长度显示是6。

请帮忙。我不知道我做错了什么。

$doc = new DOMDocument();
$doc->strictErrorChecking = false;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//td[contains(@class,"weeklyPromotionImage")]');
echo "<table>";
foreach ($nodes as $node) {
    echo "<tr><td>";
    if ($node->nodeValue=="")
    {
        echo "blank";
    }
    else
    {
        echo $node->nodeValue;
    }
    echo "</td></tr>";
}
echo "</table>";
echo "<pre>";
var_dump($nodes);
echo "</pre>";

输出结果

object(DOMNodeList)#9 (1) {
  ["length"]=>
  int(6)
}

我想检查类"每周促销图像"的 td 值。如果它是空的,我想将其样式设置为显示 None。

$node->setAttribute('style','display:none;');
您可以

做的一种方法是计算循环内该<td>标签中的子级数量。

但不包括DOMText.换行符不得计算在内。

想法:

echo "<table>";
foreach ($nodes as $node) {
    // counter
    $count = 0;
    foreach($node->childNodes as $node) {
        // if not a domtext, add count
        if(!($node instanceof 'DomText)) $count++;
    }   
    if($count > 0) {
        // no empty node
    } else {
        // is empty
        $node->addAttribute() ... // go on with your code
    }
}
echo "</table>";
相关文章: