2016-04-22 43 views
1
$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值是否为空。检查td与特定类的nodeValue是否为空

我试过下面的代码。它只是返回空结果。但它的长度显示为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值 'weeklyPromotionImage'。如果它是空的,我想设置它的样式来显示None。

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

你想无论如何要检查什么价值? – Ghost

+0

你有多个类'weeklyPromotionImage'的'​​'元素。那么,他们中的哪一个应该是空的,哪一个不应该? – RomanPerekhrest

+0

我想检查td类中的值'weeklyPromotionImage'。 E.g ''this value .. – latt

回答

0

你可以做的一种方法是计算循环内<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>"; 
+0

谢谢..这是工作..你救了我的一天.. :) – latt

+0

@latt很高兴这有助于 – Ghost

+2

@latt:请[**接受**](http://meta.stackoverflow.com/q/5234/ 234215)这个答案如果有帮助。谢谢。 – kjhughes