dj.milojevic dj.milojevic - 5 months ago 16
HTML Question

simple HTML DOM tag selecting issue

I have this HTML:

<div class="price" itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<small class="old-price">Stara cena: 1.890 RSD</small>
<span>Ušteda: <strong>1.000 RSD</strong></span>
<h5>890 <em>RSD</em>
<div class="tooltip"><p>Cene sa popustom uz gotovinsko plaćanje za online porudžbine</p></div>
</h5>
<span style="display:none" itemprop="priceCurrency" content="RSD"></span>
<span itemprop="price" content="890.00"></span>
</div>


I'm collecting prices from tag like this:

foreach($html->find('span[itemprop=price]') as $element) {
$niz['price'][] = $element->content;
}


And now i need to collect text from small tag if it exists (if it does not exist then i need empty string in an array):
<small class="old-price">Stara cena: 1.890 RSD</small>


So i need something like this:

if($html->find('small[class=old-price]',0))
{
$niz['oldprice'][] = $element->innertext;
}else{
$niz['oldprice'][] = '';
}


Problem is that i get only elements from class=old-price in array and not a single empty string.

Any advice would be appreciated.

Answer

Hi can you please use code

foreach($html->find('small[class=old-price]')  as $element) {
    if($element->plaintext)
    {
         $niz['oldprice'][] = $element->plaintext;
    }else{
         $niz['oldprice'][] = '';
    }
}