ajameswolf
ajameswolf

Reputation: 1660

Preg Match Between Curly Braces - preg_match

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/

Upvotes: 3

Views: 6911

Answers (1)

hek2mgl
hek2mgl

Reputation: 157992

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

Upvotes: 12

Related Questions