尝试这个 :
$str = '1 x Book @ $20 32 x rock music cd @ $400 1 x shipping to india @ $10.5';
preg_match_all('/(?P<match>\d+\s+x\s+[\w\s]+\s+@\s+\$[\d\.]+)/',$str,$match);
echo "<pre>";
print_r($match['match']);
输出 :
Array
(
[0] => 1 x Book @ $20
[1] => 32 x rock music cd @ $400
[2] => 1 x shipping to india @ $10.5
)
另一个解决方案:
根据 Dream Eater 的评论:更小的写法:(.*?\$\d+)\s。– Dream Eater 3 分钟前
我只是*
在最后添加了它,它工作正常。
$str = '1 x Book @ $20 32 x rock music cd @ $400 1 x shipping to india @ $10.5';
preg_match_all('/(.*?\$\d+)\s*/',$str,$match);
echo "<pre>";
print_r($match);
参考: http: //php.net/manual/en/function.preg-match.php