Preg_match finds content between a specific div with a class

How can I find the contents of this div with preg_match ? <div class="lot-price-block">

I did something similar, but can't get anything:

$value=preg_match_all('/<div class=\"lot\-price\-block\">(.*)<\/div>/',$file_contents,$estimates);
+5
source share
2 answers

First of all, make your regex less greedy using an operator ?. This will allow you not to get more than what you think you will receive. The following is the statement /sat the end of your expression. I assume that you are parsing an HTML file that contains many new " \n" string characters .

$value=preg_match_all('/<div class=\"lot\-price\-block\">(.*?)<\/div>/s',$file_contents,$estimates);
+14
source

Try the following:

$file_contents = '<div class="lot-price-block">Test content</div>';

$value=preg_match_all('/<div\s*class="lot\-price\-block"\s*>(?P<content>.*)<\/div>/',$file_contents,$estimates);

echo "<pre>";
print_r($estimates);

You will receive all agreed content in print_r($estimates['content']);

0
source

All Articles