【发布时间】:2013-04-30 16:03:20
【问题描述】:
注意:我最近问过这个问题,但事实证明我正在寻找的解决方案比我最初想象的要先进。
使用 preg_split,我将如何拆分它,注意分隔符之前的字符串会有所不同:
$string = "big red apple one purple grape some stuff then green apple yatta yatta red cherry green gape";
我想使用一个字符串数组作为分隔符,我希望它们包含在结果中。分隔符:[苹果、葡萄、樱桃]
我想要的输出是:
Array("big red apple", "one purple grape", "some stuff then green apple", "yatta yatta red cherry", "green grape");
这是我最初的:
$string = "big red apple one purple grape some stuff then green apple yatta yatta red cherry green gape";
$matches = preg_split('(apple|grape|cherry)', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($matches);
它打印这个: 数组([0] => 大红色 [1] => 一个紫色 [2] => 一些东西,然后是绿色 [3] => yatta yatta red [4] => green gape)
没有分隔符。
【问题讨论】:
-
你了解 ReGeX(读作 re-jek)吗?你试过什么吗?
标签: php arrays preg-split