您可以使用preg_split
代替
$text = "abc/text text def/long amount of text ghi/some text";
$output = preg_split("/(abc\/|def\/|ghi)/", $text);
var_dump($output);
输出:
array(4) {
[0]=>
string(0) ""
[1]=>
string(10) "text text "
[2]=>
string(20) "long amount of text "
[3]=>
string(10) "/some text"
}
更新:(删除空项目,并重新索引)
$output = array_values(array_filter(preg_split("/(abc\/|def\/|ghi)/", $text)));
var_dump($output);
输出:
array(3) {
[0]=>
string(10) "text text "
[1]=>
string(20) "long amount of text "
[2]=>
string(10) "/some text"
}
DEMO.
更新:(2013年9月26日)
$str = "abc/text text def/long amount of text ghi/some text";
$array = preg_split("/([a-z]{3}\/)/", $str, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$odd = $even = array();
foreach($array as $k => $v)
{
if ($k % 2 == 0) $odd[] = $v;
else $even[] = $v;
}
$output = array_combine($odd, $even);
print_r($output);
输出:
Array (
[abc/] => text text
[def/] => long amount of text
[ghi/] => some text
)
DEMO.
更新:(2013年9月26日)
你可以试试这个问题,以及(只更改以下行来实现您在评论中提及的结果)
$array = preg_split("/([a-zA-Z]{1,4}\/)/", $str, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
DEMO.
你能给我们一个确切的输入例子吗? –
你的价值观在哪里? – SamT