【问题标题】:Check td nodeValue with specific class is empty or not检查特定类的 td nodeValue 是否为空
【发布时间】:2016-08-15 14:30:22
【问题描述】:
    $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>
';

我想检查 'weeklyPromotionImage' 类的 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)
}

我想检查“weeklyPromotionImage”类的 td 值。如果它是空的,我想将它的样式设置为显示无。

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

【问题讨论】:

  • 你到底要检查什么值?
  • 您有多个 &lt;td&gt; 元素,其类为 weeklyPromotionImage。那么,其中哪些应该是空的,哪些 - 不应该?
  • 我想用 'weeklyPromotionImage' 类检查 td 内的值。例如 '' 这个值..
  • 我想将属性设置为 td 样式,使用 weekPromotionImage 将空值设置为 'display:none';

标签: php xml xpath nodevalue


【解决方案1】:

您可以做的一种方法是计算循环内&lt;td&gt; 标记内的子项数。

但不要包含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>";

【讨论】:

  • @latt 很高兴这有帮助
  • @latt:如果有帮助,请accept这个答案。谢谢。
猜你喜欢
  • 2014-05-25
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
相关资源
最近更新 更多