PHP中的初学者正则表达式。一些讨厌的角色

PHP中的初学者正则表达式。一些讨厌的角色,php,regex,Php,Regex,我需要将VB.NET中的一些代码重写为PHP: Dim price as String = Regex.Match(html, "id=""price"">&pound;\d+.\d+</span>").Value.Replace("id=""Acashprice"">&pound;", "").Replace("</span>", "") Dim price as String=Regex.Match(html,“id=”“price”“>

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

Dim price as String = Regex.Match(html, "id=""price"">&pound;\d+.\d+</span>").Value.Replace("id=""Acashprice"">&pound;", "").Replace("</span>", "")
Dim price as String=Regex.Match(html,“id=”“price”“>£;\d+。\d+”).Value.Replace(“id=”“Acashprice”“>£;”,“”)。Replace(“,”)
因此,我试图从正则表达式中获取匹配项开始:

id="price">&pound;\d+.\d+</span>
id=“price”>£\d+。\d+

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

这应该可以完成以下工作:

preg_match('/(?<=id="price">)&pound;\d+.\d+/', '<span id="price">&pound;55.55</span>', $m);
print_r($m);
更可靠的正则表达式如下所示:

$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]);

向我们展示PHP代码。正则表达式尝试匹配价格标签并插入一些自定义HTML。您可能应该转义正则表达式中的引号和
/
符号(任何在正则表达式中有意义的特殊字符)!正则表达式所做的就是找到下载页面中我感兴趣的部分。到目前为止,我得到的唯一代码是:preg_match(“id=“Acashprice”>£;\d+。\d+”,$page,$matches);$price=$matches[0];我知道这是错的。哦,这是一个下载页面!最好从阅读开始!!!!谢谢那真的很有帮助。:)
$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
)