[英]php preg match parse html
I am trying to get 1,9110 from the string: 我想从字符串中获得1,9110 :
<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 你不需要一个正则表达式,你可以使用带有特定标志的
filter_var
来容纳数千个
echo filter_var('<span class="c_name">€ EUR</span> <span class="c_value">1,9110</span>', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.