说我有一个字符串
$what = "1 x Book @ $20 32 x rock music cd @ $400 1 x shipping to india @ $10.5";
我想要爆炸,以便输出
Array
(
[0] => 1 x Book @ $20
[1] => 32 x rock music cd @ $400
[2] => 1 x shipping to india @ $10.50
)
我在想下面的内容,但不知道要使用哪些正则表达式!
$items = preg_split('/[$????]/', $what);
提前致谢
答案 0 :(得分:3)
试试这个:
$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);