0

How can I get price 1.199,00 from below html using preg_match_all?

`<h4><span class="price_label">Preis:&nbsp;</span>1.199,00 Euro &nbsp;(inkl. 19% MwSt.)</h4>`

Code

<?php
     $pattern = '#'.$regex.'#';
     preg_match_all($pattern, $data, $price);
     print_r(price);
    ?>
1
  • Please show us the Regex that you tried. Commented Jan 6, 2017 at 12:25

1 Answer 1

1

This would be a simple example:

<?php
$subject = '<h4><span class="price_label">Preis:&nbsp;</span>1.199,00 Euro &nbsp;(inkl. 19% MwSt.)</h4>';
$pattern = '|<span[^>]+class="price_label".*>[^<]+</span>([0-9.,]+)\s*.*$|';
preg_match($pattern, $subject, $tokens);
var_dump($tokens[1]);

The output obviously is:

string(8) "1.199,00"

Note however that it is questionable to use regular expressions to parse HTML markup or extract values from it. Such solutions tend to be picky, so not robust against minor modifications of the markup. It is far better to use a DOM parser.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.