将PHP字符串分成基于" divider"的排列子字符串。人物

时间:2015-04-01 19:16:15

标签: php arrays regex string substring

我想要做的是将PHP字符串拆分为一组子字符串,这些字符串根据开始这些子字符串的“divider”字符分组为数组。字符*^%保留为分隔符。因此,如果我有字符串"*Here's some text^that is meant*to be separated^based on where%the divider characters^are",它应该被拆分并放在如下的数组中:

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" 
}

我完全失去了这个。有谁知道如何实现这个?

1 个答案:

答案 0 :(得分:3)

如果您愿意,请不要问$matches[0]如此取消设置:

preg_match_all('/(\*[^*^%]+)|(\^[^*^%]+)|(%[^*^%]+)/', $string, $matches);
$matches = array_map('array_filter', $matches);

print_r($matches);

array_filter()从捕获组子数组中移除空白以提供问题中显示的数组

相关问题