-1

Here is a part from my html string.

<span class="price">£ 343</span>
// Some html code
<span class="price" id="old-price-22898">£ 343</span>
</p><p class="special-price">
<span class="price" id="product-price-22898"> £ 274</span> 

What I want is to get all the prices.

So I tried this regexp :

<span class=\"price\"(.*)>(.*)<\/span>

which makes sense to me, but I only get the price between <span class="price"> and not the prices between the <span> with ids.

Any help ?

Stephan
  • 4,187
  • 1
  • 21
  • 33
  • 1
    Better not parse HTML with regexps http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – user4035 Jul 07 '14 at 11:40
  • You should work out better question titles. Just throwing a few tags into the title won't be helpful for further use. – pdu Jul 07 '14 at 11:50

2 Answers2

1

Alternatively, you could also use DOMDocument with xpath. Consider this example:

$html_string = '<span class="price">£ 343</span><span class="price" id="old-price-22898">£ 343</span></p><p class="special-price"><span class="price" id="product-price-22898"> £ 274</span>';
$html_string = mb_convert_encoding($html_string, 'html-entities', 'utf-8'); 
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->substituteEntities = TRUE;
libxml_use_internal_errors(true);
$dom->loadHTML($html_string);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$prices = array();
foreach($xpath->query('//*[@class="price"]') as $price) {
    $prices[] = $price->nodeValue;
}

echo '<pre>';
print_r($prices);

Output:

Array
(
    [0] => £ 343
    [1] => £ 343
    [2] =>  £ 274
)
user1978142
  • 7,946
  • 3
  • 17
  • 20
0

Below regex would capture the id's and prices in <span class="price"> tag and <span> tag.

<span class=\".*?(?:(id=[^>]*))?>\s*([^<]*)\s*

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274