1

I am trying to get 1,9110 from the string:

<span class="c_name">€ EUR</span> <span class="c_value">1,9110</span>

And my code is:

// EUR //
if(preg_match('/<span class="c_name">€ EUR<\/span> <span class="c_value">(.*?)<\/span>/mis', $rawresult, $result))
{
    $banks['access']['sale']['EUR'] = $result[1];
} else {
    $banks['access']['sale']['EUR'] = false;
}
var_dump($banks);
// EUR //

But this code isn't working

7
  • Umm, yes it is? $result[1] = "1,9110". Commented Mar 11, 2016 at 13:39
  • no not worked. Warning: preg_match(): Compilation failed: invalid UTF-8 string at offset 21 Commented Mar 11, 2016 at 13:39
  • egg I updated my code Commented Mar 11, 2016 at 13:41
  • run a htmlspecialchars() on the string and use &euro; in the match Commented Mar 11, 2016 at 13:43
  • not worked for me Commented Mar 11, 2016 at 13:45

3 Answers 3

3

You don't need a regular expression for this, you can just use filter_var with a specific flag to allow the thousands

echo filter_var('<span class="c_name">€ EUR</span> <span class="c_value">1,9110</span>', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);

https://eval.in/534892

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

Comments

0

Your regex crashes on the euro-sign. Try to escape it like this:

if(preg_match('/<span class="c_name">\x{20AC} EUR<\/span> <span class="c_value">(.*?)<\/span>/misu', $rawresult, $result))

2 Comments

Warning: preg_match(): Compilation failed: character value in \x{} or \o{} is too large at offset 28 in
Yup, forgot to add u as param, added it just now.
0
'<span class="c_value">(.*?)<\/span>/s';

or try

if(preg_match('/<span class="c_name">.*?EUR<\/span> <span class="c_value">(.*?)<\/span>/mis', $rawresult, $result))

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.