根据“divider”字符将PHP字符串划分为排列的子字符串
我想要做的是将一个PHP字符串分成一组子字符串,这些子字符串根据开始这些子字符串的“divider”字符分组为数组。字符*
,^
和%
被保留为分隔符。所以,如果我有串"*Here's some text^that is meant*to be separated^based on where%the divider characters^are"
,应该分裂并放置在阵列像这样:根据“divider”字符将PHP字符串划分为排列的子字符串
array(2) {
[0] => "*Here's some text"
[1] => "*to be separated"
}
array(3) {
[0] => "^that is meant"
[1] => "^based on where"
[2] => "^are"
}
array(1) {
[0] => "%the divider characters"
}
我完全失去了在这一个。有谁知道如何实现这一点?
,如果你想你不为$matches[0]
所以取消它问:
preg_match_all('/(\*[^\*\^%]+)|(\^[^\*\^%]+)|(%[^\*\^%]+)/', $string, $matches);
$matches = array_map('array_filter', $matches);
print_r($matches);
的array_filter()
去除捕获组子阵列空瓶给在这个问题
'array_map('array_filter'' really?why not'unset'? – 2015-04-01 19:29:09
'array_filter'去除其他空容器。这将是一个循环和多个unsets – AbraCadaver 2015-04-01 19:29:53
另一个所示的阵列方法(优化)..
$matches = array();
preg_match_all('/[\*\^\%][^\*\^\%]+/', $str, $matches);
var_dump($matches);
你试过explode()函数吗? – Maximus2012 2015-04-01 19:21:05
“分隔符”的编程术语是分隔符。 – 2015-04-01 19:38:03
感谢您的提示。我不知道。 – vdubguy777 2015-04-01 20:09:52