这应该做的工作:
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
)