Skip to content
Advertisement

How to get price value with regular expressions

I am trying to write a crawler for an Online Store and now I need to get the price value of the webpage. Here is my try:

$match = "";
$isMatched = preg_match('|<div class="c-product__seller-price-pure js-price-value".*$(.*)<|',$html,$match);
echo "<pre>";
print_r($match);
echo "</pre>";

Basically $html holds the source code of the webpage and the price value is stored at the document like this:

<div class="c-product__seller-price-pure js-price-value">10,699,000</div>

But when I run this I get this as result:

Array
(
)

Meaning that, the regular expression that I’m using is not correct:

|<div class="c-product__seller-price-pure js-price-value".*$(.*)<|

So how can I get this price value with regex properly?

Advertisement

Answer

You’re currently matching any character .* after js-price-value” with a dollar sign `$. But there’s no dollar sign in your source HTML.

Drop the $ sign and match anything between the opening and closure of the tag.

Easiest way is to use the NOT operator ^, to match any character that is not the < sign, like this ([^<]+)

See below complete code:

$match = "";
$isMatched = preg_match('|c-product__seller-price-pure js-price-value">([^<]+)|', $html, $match);
echo "<pre>";
print_r($match);
echo "</pre>";
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement