根据"divider"个字符将PHP个字符串分成数组子串

Divide PHP string into arrayed substrings based on "divider" characters

我想做的是将 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" 
}

我完全迷失在这一点上。有谁知道如何实现这个?

您不需要 $matches[0],因此如果需要,请取消设置:

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

print_r($matches);

array_filter() 从捕获组子数组中删除空值以给出问题中显示的数组