我需要能够在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)
我真的被困了,谢谢......
答案 0 :(得分: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);