2016-04-22 3 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値が空であるかどうかをチェックしたいと思います。特定のクラスを持つ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'でチェックしたいと思います。たとえば、 ''この値.. – 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

@ラット確かにこれは助けて – Ghost

+2

@ラット:[**受け入れる**](http://meta.stackoverflow.com/q/5234/ 234215)それが助けられたらこの答え。ありがとう。 – kjhughes

関連する問題