从目标字符串中选择除空格以外的所有文本


Select all text from targeted string excluding whitespace

>我有一个字符串,如下所示

&pound;&nbsp;                               0.00<br>

我只对提取在 £<br> 标签之间的字符串中找到的十进制值感兴趣。我目前有一个正则表达式,即:

(?<=&pound;&nbsp;)(.*?)(?='<br>)

给出以下结果

                       0.00

我需要确保最终结果中不包含空格,我尝试了如下方法......

(?<=&pound;&nbsp;'s*)(.*?)(?='<br>)

这显然是错误的,意味着我不知道我在做什么。

如何确保提取正确的十进制值减去任何空格?

e.g. 
0.00
instead of 
           0.00

trim()生成的字符串?

$result = trim($result);

如果您只对十进制值感兴趣,则正则表达式模式应如下所示。该示例打印在搜索字符串中找到的所有小数。

<?php
$string = '&pound;&nbsp;
                          5.00<br><br><br>
                          Shipping&nbsp;&pound;&nbsp;3.35<br><br><b>Total&nbsp;&pound;&nbsp;
                             8.35<br></b>';
$pattern = '/&pound;&nbsp;'s*(-?[0-9]+'.[0-9]+)<br>/u';
$result = preg_match_all($pattern, $string, $matches);
if($result === FALSE) {
    die('error in regex');
}
// output the decimals
if($result > 0) {
    foreach($matches[1] as $decimal) {
        echo $decimal, PHP_EOL;
    }
}
// Output:
//
// 5.00
// 3.35
// 8.35

请注意,该模式将匹配正小数和负小数

为什么不简化正则表达式?

/&pound;&nbsp;'s*([0-9'.]+)<br>/u

更新:更一般的情况:

/&pound;.*([0-9'.]+)<br>/u

这有效;

$s = '&pound;&nbsp;                               0.00<br>';
preg_match('~&(.*?);'s+(['d'.]+)~i', $s, $m);
// or
// preg_match('~&('w+);'s+(['d'.]+)~i', $s, $m);
print_r($m);

外;

数组(    [0] => 磅;                              0.00    [1] => 磅;&nbsp    [2] => 0.00)