php xpath: Bir sorgu sonucu içinde sorgu

0 Cevap php

Ben bir html dosyasını ayrıştırmak çalışıyorum.

Fikir yayılma en title ve desc sınıfları almak ve nitelik class = 'theBest' olan her div onların bilgilerini almak için olduğunu.

İşte benim kod:

<?php

$example=<<<KFIR
<html>
<head>
<title>test</title>
</head>
<body>
 <div class="a">moshe1
<div class="aa">haim</div>
 </div>
 <div class="a">moshe2</div>
 <div class="b">moshe3</div>

<div class="thebest">
<span class="title">title1</span>
<span class="desc">desc1</span>
</div>
<div class="thebest">
span class="title">title2</span>
<span class="desc">desc2</span>
</div>

</body>
</html>
KFIR;


$doc = new DOMDocument();
@$doc->loadHTML($example);
$xpath = new DOMXPath($doc);
$expression="//div[@class='thebest']";
$arts = $xpath->query($expression);

foreach ($arts as $art) {
    $arts2=$xpath->query("//span[@class='title']",$art);
    echo $arts2->item(0)->nodeValue;
    $arts2=$xpath->query("//span[@class='desc']",$art);
    echo $arts2->item(0)->nodeValue;
}
echo "done";

beklenen sonuçlar şunlardır:

title1desc1title2desc2done 

Ben alıyorum sonuçları:

title1desc1title1desc1done

0 Cevap