Ricardo Mehr
Ricardo Mehr

Reputation: 310

PHP simple html dom, selecting just a part of the content

Im using simple html dom to parse html. https://simplehtmldom.sourceforge.io/

Given the following code

<div class="item-price offer-price price-tc default-price">
$129.990
<span class="discount-2">-35%</span>
</div>

How can I select just the price? Im using $html->find(div.offer-price, 0)->plaintext; but it selects the content of the span too.

Upvotes: 0

Views: 183

Answers (1)

hanshenrik
hanshenrik

Reputation: 21463

not sure how to do it in simplehtmldom, but you can use DOMDocument + DOMXPath to extract it,

<?php

$html='<div class="item-price offer-price price-tc default-price">
$129.990
<span class="discount-2">-35%</span>
</div>
';
echo  (new DOMXPath(@DOMDocument::loadHTML($html)))->query("//div[contains(@class,'item-price')]/text()")->item(0)->textContent;

bonus: both DOMDocument and DOMXPath are php builtins, no external library required to use em

Upvotes: 1

Related Questions