Search code examples
phpweb-scrapingsimple-html-dom

PHP SimpleHTMLDOM : how to get value from div by class name inside of another div?


i have following HTML snippet:

<div class="price-box szczeg_oper">
            <div class="row">
              <div class="col c-1-2 c-xs-1-1">
                <div class="price netto">cena: 11.30 <span class="currency">CZK</span></div>                <div class="ad_koszyk row">
          <div class="col c-1-2 c-xs-1-2">
            <div class="quantity lng_cz">        
              <label>množství</label>
              <input type="text" size="1" id="quantity_178" name="quantity" value="1" data-cip-id="quantity_178">
            </div>
          </div>
          <div class="col c-1-2 c-xs-1-2">
            <input type="hidden" name="parametersValue" value="">
            <input class="button pull-right" name="przyciskz" type="submit" value="Do košíku" onclick="ajax_cart_add(178, jQuery('#quantity_178').val(), 'cz', 0.115); return false;">
          </div>
         </div>              </div>
              <div class="col c-1-2 c-xs-1-1">
                                <div class="product-availability">Zboží je k dispozici na skladě</div>
              </div>
            </div>
          </div>

I'm trying to get value inside the div.

Div with same class is contained in the document a few times. So i need to get only this one inside of the div

If i tried this:

 //GET NETTO PRICE
    foreach($html->find('div[class=price-box szczeg_oper]') as $element){
        $itemPrice = $element->plaintext;
    }

I got text of whole div.

How can i access into inner elements inside of the found div by class?

I tried to find solution in documentation, but without luck:

http://simplehtmldom.sourceforge.net/manual.htm

Many thanks for any advice


Solution

  • I'm assuming you're after the price

    foreach($html->find('div[class=price-box szczeg_oper]') as $element){
        $itemPrice = $element->find('.price',0)->plaintext;
    }