fork download
  1. <?php
  2.  
  3. $re = "#(?=(\{(?>[^{}]|(?1))*+\}))#";
  4. $str = "Hello {#name}! I'm a {%string|sentence|bit of {#random} text}";
  5. preg_match_all($re, $str, $matches, PREG_OFFSET_CAPTURE);
  6. print_r($matches[1]);
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [0] => {#name}
            [1] => 6
        )

    [1] => Array
        (
            [0] => {%string|sentence|bit of {#random} text}
            [1] => 21
        )

    [2] => Array
        (
            [0] => {#random}
            [1] => 46
        )

)