2011-01-26 6 views
2

Je viens de commencer à utiliser PHP Simple HTML DOM Parser.PHP Simple HTML DOM Parser

Maintenant j'essaye d'extraire tous les éléments entourés d'un <b> -tag inclurant </b> d'un document HTML exsiting. Cela fonctionne très bien avec

foreach($html->find('b') as $q) 
    echo $q; 

Comment puis-je obtenir de montrer que des éléments entourés des <b>, </b> -tags suivi d'un <span class="marked">?

Mise à jour: J'ai utilisé firebug pour obtenir le chemin css des éléments. Maintenant, il ressemble à ceci:

foreach ($html->find('html body div#wrapper table.desc tbody tr td div span.marked') as $x) 
    foreach ($x->find('html body div#wrapper table.desc tbody tr td table.split tbody tr td b') as $d) 
     echo $d; 

Mais cela ne fonctionnera pas ... Des idées?

Mise à jour:

Pour clarifier ma question ici un échantillon tr du document avec table début et de fin des balises de table.

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="desc"> 
    <tr> 
     <th width="25%" scope="col"><div align="center">1</div></th> 
     <th width="50" scope="col"><div align="center">2</div></th> 
     <th width="10%" scope="col"><div align="center">3</div></th> 
     <th width="15%" scope="col"><div align="center">4</div></th> 
    </tr> 
    <tr> 
     <td valign="top" bgcolor="#E9E9E9"><div style="text-align: center; font-weight: bold; margin-top: 2px"> 1 </div></td> 
     <td> 
      <table width="100%" border="0" cellspacing="0" cellpadding="0" class="split"> <tr> 
        <td> 
         <b> element to extract</b></td> 
       </tr> 
       <tr> 
        <td> 
         <table width="100%" border="0" cellspacing="0" cellpadding="0" class="split"> <tr> 
           <td width="15px" valign="top">&nbsp;</td> 
           <td width="15px" valign="top"> 
            <div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px"> 
             1 
            </div> 
           </td> 
           <td> 
            abed 
           </td> 
          </tr> 
          <tr> 
           <td width="15px" valign="top">&nbsp;</td> 
           <td width="15px" valign="top"> 
            <div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px"> 
             2 
            </div> 
           </td> 
           <td> 
            ddee 
           </td> 
          </tr> 
          <tr> 
           <td width="15px" valign="top">&nbsp;</td> 
           <td width="15px" valign="top"> 
            <div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px"> 
             3 
            </div> 
           </td> 
           <td> 
            xdef 
           </td> 
          </tr> 
          <tr> 
           <td width="15px" valign="top">&nbsp;</td> 
           <td width="15px" valign="top"> 
            <div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px"> 
             4 
            </div> 
           </td> 
           <td> 
            abbcc 
           </td> 
          </tr> 
          <tr> 
           <td width="15px" valign="top">&nbsp;</td> 
           <td width="15px" valign="top"> 
            <div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px"> 
             5 
            </div> 
           </td> 
           <td> 
            ab 
           </td> 
          </tr> 
          <tr> 
           <td width="15px" valign="top">&nbsp;</td> 
           <td width="15px" valign="top"> 
            <div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px"> 
             6 
            </div> 
           </td> 
           <td> 
            e1 
           </td> 
          </tr> 
         </table> 
        </td> 
       </tr> 
      </table> 
     </td> 
     <td valign="top"><div style="text-align: center"> <span class="marked">marked</span> </div></td> 
     <td valign="top"><div style="text-align: center"> </div></td> 
    </tr> 
</table> 
+0

Voulez-vous dire '' 'ou '? –

Répondre

3

Essayez le CSS selector suivant

b > span.marked 

Cela renverrait la durée, donc vous avez probablement faire $e->parent() pour se rendre à l'élément b.

Voir aussi Best Methods to parse HTML des alternatives à SimpleHtmlDom


Modifier après mise à jour:

Your browser will modify the DOM. If you look at your markup, vous verrez qu'il n'y a pas d'éléments tbody. Pourtant, Firebug vous donne

html body div#wrapper table.desc tbody tr td div span.marked' 
html body div#wrapper table.desc tbody tr td table.split tbody tr td b' 

De plus, votre question ne correspond pas aux requêtes. Vous avez demandé comment trouver

éléments entourés des <b>,</b> -tags suivie d'une <span class="marked">

qui peut être lu soit signifie

<b><span class="marked">foo</span></b> 

ou

<b><element>foo</element></b><span class="marked">foo</span> 

Pour cette première utilisation, le child combinator I ont montré plus tôt. Pour la seconde, utilisez le adjacent sibling combinator

b + span.marked 

pour obtenir la durée puis utilisez $e->prev_sibling() pour retourner le frère précédent d'un élément (ou null si introuvable). Cependant, dans votre balisage affiché, il n'y a ni.Il y a seulement un DIV avec un enfant de la classe SPAN ayant marqué

<div style="text-align: center"> <span class="marked">marked</span> 

Si tel est ce que vous voulez faire correspondre, il est le combinateur de l'enfant à nouveau. Bien sûr, vous devez changer le b puis à un div.

+0

à droite. Vous pouvez utiliser des sélecteurs CSS pour HTML DOM Parser. – Eray

+0

@Eray Je ne sais pas jusqu'à quel niveau SimpleHtmlDom les implémente bien. Et tbh, je ne vois pas pourquoi je aurais besoin d'eux (ou SimpleHtmlDom) quand je peux utiliser DOM et XPath :) – Gordon

+0

FTR, simplehtmldom ne supporte pas les sélecteurs de frère, mais [certaines alternatives font.] (Http: //scraperblog.blogspot .com/2012/11/choose-php-html-parser.html) – pguardiario

-1

Plus simple est de manuel:

foreach($html->find('b') as $q) 
    echo $q->plaintext; 
+0

OP ne veut que les balises suivies de '' – MarcDefiant