Preg Match Between Curly Braces - preg_match

php, regex

Solution

Check the following code:

$str = '{this}{is}a{my} {example}{{subject}}';

if(preg_match_all('/{+(.*?)}/', $str, $matches)) {
    var_dump($matches[1]);
}

Output:

array(5) {
  [0] =>
  string(4) "this"
  [1] =>
  string(2) "is"
  [2] =>
  string(2) "my"
  [3] =>
  string(7) "example"
  [4] =>
  string(7) "subject"
}

Problem

Example Subject: {this}{is}a{my} {example}{{subject}} To return: ``` array( [1] => 'this' [2] => 'is' [3] => 'my' [4] => 'example' [5] => 'subject' ``` This is common in php templating engines cite smarty and twig http://www.smarty.net/ http://twig.sensiolabs.org/

Original source