我需要将一些代码从VB.NET重写为PHP:
Dim price as String = Regex.Match(html, "id=""price"">£\d+.\d+</span>").Value.Replace("id=""Acashprice"">£", "").Replace("</span>", "")
所以我试图从正则表达式获得一个匹配:
id="price">£\d+.\d+</span>
然而,无论我如何格式化它,我总是被告知它是无效的 - (即不允许反斜杠,或者什么是p?)。我想我可能必须将preg_quote与preg_match结合使用,但我也无法使用它。任何帮助将不胜感激。
答案 0 :(得分:2)
这应该做的工作:
preg_match('/(?<=id="price">)£\d+.\d+/', '<span id="price">£55.55</span>', $m);
print_r($m);
<强>输出:强>
Array
(
[0] => £55.55
)
更可靠的正则表达式如下:
$str = '<span id="price">£11.11</span>
<span id="price">£22</span>
<span id="price"> £ 33 </span>
<span id = "price" > £ 44 </span>
<span id=\'price\' > £ 55 </span>
<span class="component" id="price"> £ 67.89 </span>
<span class="component" id="price" style="float:left"> £ 77.5 </span>
<span class="component" id="price" style="float:left:color:#000"> £77.5 </span>
';
preg_match_all('/<span.+?id\s*=\s*(?:"price"|\'price\').*?>\s*((?:£|£)\s?\d+(?:.\d+)?)\s*<\/span>/is', $str, $m);
print_r($m[1]);
<强>输出:强>
Array
(
[0] => £11.11
[1] => £22
[2] => £ 33
[3] => £ 44
[4] => £ 55
[5] => £ 67.89
[6] => £ 77.5
[7] => £77.5
)