php从字符串中剥离非货币字符

时间:2015-03-10 16:02:45

标签: php regex

我有这个字符串

From: £5.95 (per linear metre)

我只想保留£5.95

我知道这是用preg_replace完成的但是怎么做?

$string = "From: £5.95 (per linear metre)";
    echo preg_replace("", "", $string);

2 个答案:

答案 0 :(得分:7)

preg_replace('/[^0-9.£]/', '', $string);

答案 1 :(得分:2)

这应该适合你:

<?php

    $str = "From: £5.95 (per linear metre)";
    preg_match_all("/From:\s(.*\d+)/", $str, $matches);
    echo $matches[1][0];

?>