0

我需要将 VB.NET 中的一些代码重写为 PHP:

Dim price as String = Regex.Match(html, "id=""price"">&pound;\d+.\d+</span>").Value.Replace("id=""Acashprice"">&pound;", "").Replace("</span>", "")

所以我试图从正则表达式中获取匹配开始:

id="price">&pound;\d+.\d+</span>

然而,无论我如何格式化它,我总是被告知它是无效的——(即不允许使用反斜杠,或者什么是 p?)。我想我可能必须将 preg_quote 与 preg_match 结合使用,但我也无法使其正常工作。任何帮助将非常感激。

4

1 回答 1

2

这应该做的工作:

preg_match('/(?<=id="price">)&pound;\d+.\d+/', '<span id="price">&pound;55.55</span>', $m);
print_r($m);

输出:

Array
(
    [0] => &pound;55.55
)

更可靠的正则表达式如下:

$str = '<span id="price">&pound;11.11</span>
<span id="price">&pound;22</span>
<span id="price"> &pound; 33 </span>
<span      id  =  "price"   >      &pound; 44     </span>
<span      id=\'price\'   >      &pound; 55     </span>
<span class="component" id="price"> &pound; 67.89 </span>
<span class="component" id="price" style="float:left"> &pound; 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*((?:&pound;|£)\s?\d+(?:.\d+)?)\s*<\/span>/is', $str, $m);

print_r($m[1]);

输出:

Array
(
    [0] => &pound;11.11
    [1] => &pound;22
    [2] => &pound; 33
    [3] => &pound; 44
    [4] => &pound; 55
    [5] => &pound; 67.89
    [6] => &pound; 77.5
    [7] => £77.5
)
于 2013-04-16T22:00:11.667 回答