PHP在分隔符之间提取字符串允许重复

I'm trying to get the text between two delimiters and save it into an array. I wrote this function, the problem with this code is it removes duplicates so

$this->getInnerSubstring('{2}{A}{A}{A}{X}','{', '}');

returns an array that is like

[0] =>2,
[1]=>A,
[2] =>X ,

Yet I want:

[0] =>2,
[1]=>A,
[2]=>A,
[3]=>A,
[4] =>X,

Without Regex patterns is there a substr flag that lets me keep duplicates? what's the best approach here:

function getInnerSubstring($string,$start, $end){
    $s = array();
        do
         {
             $startpos = strpos($string, $start) + strlen($start);
             $endpos = strpos($string, $end, $startpos);
             $s[] = substr($string, $startpos, $endpos - $startpos);
                //remove entire occurance from string:
                $string =   str_replace(substr($string, strpos($string, $start), strpos($string, $end) +strlen($end)), '', $string);


        }
    while (strpos($string, $start)!== false && strpos($string, $end)!== false);


    return $s;

    }

Use preg_match_all() for that:

$string = "{2}{A}{A}{A}{X}";
$ldelim = "{";
$rdelim = "}";

var_dump(getInnerSubstring($string, $ldelim, $rdelim));

function getInnerSubstring($string, $ldelim, $rdelim) {
    $pattern = "/" . preg_quote($ldelim) . "(.*?)" . preg_quote($rdelim) . "/";
    preg_match_all($pattern, $string, $matches);
    return $matches[1];
}

output:

array(5) {
  [0]=>
  string(1) "2"
  [1]=>
  string(1) "A"
  [2]=>
  string(1) "A"
  [3]=>
  string(1) "A"
  [4]=>
  string(1) "X"
}

An alternative would be to use preg_split():

var_dump(preg_split('({|})', $string, -1, PREG_SPLIT_NO_EMPTY));

You can put that into a function using the same way as above.

Nice and simple:

Just changed all { and } to { and exploded it on it.

$str = "{2}{A2}{A}{A}{X}";
$str = array_filter(explode("{", str_replace(["{", "}"], ["", "{"], $str)));
print_r($str);

output:

Array ( [0] => 2 [1] => A2 [2] => A [3] => A [4] => X )

In order to be able to recognize empty elements you could use:

$str = "{2}{A2}{A}{A}{}{X}";
$elements = explode('}', str_replace("{", "", $str) . PHP_EOL));
array_pop($elements);
print_r($elements);

output:

Array ( [0] =>  2  [1] =>  A2  [2] =>  A  [3] =>  A  [4] =>  [5] =>  X )