0

我需要能够在 PHP 中提取以下内容,这让我发疯。

以下面的字符串为例:

$text = "Nalgene Narrow-Mouth Loop-Top 16-oz. Water Bottle for $4 + pickup and %40 off at REI";

一方面,我需要能够提取:

$4 as extracted currency

%40 as extracted percentage

并区分哪个是货币,哪个是百分比。

为了让它更复杂一点,如果货币是,我也应该抓住它:

$4.11 or $ 4.10 (not the spacing and the comma)

或者如果百分比是:

% 40 (note the spacing)

我真的卡住了,谢谢..

4

1 回答 1

1
$text = "Nalgene Narrow-Mouth Loop-Top 16-oz. Water Bottle for $4.3 + pickup and % 40 off at REI";
preg_match('/%([ 0-9]+?) /', $text, $matches);
print_r($matches);
preg_match('/\$([ .0-9]+?) /', $text, $matches);
print_r($matches);
于 2013-04-13T10:03:21.357 回答