简体   繁体   中英

php preg match parse html

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

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

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))
'<span class="c_value">(.*?)<\/span>/s';

或尝试

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM