卷曲大括号之间的匹配 - preg_match

aja*_*olf 2 php regex

示例主题:{this} {is} a {my} {example} {{subject}}要返回:

array(
[1] => 'this' 
[2] => 'is' 
[3] => 'my'
[4] => 'example'
[5] => 'subject'
Run Code Online (Sandbox Code Playgroud)

这在php模板引擎中常见,引用smarty和twig http://www.smarty.net/ http://twig.sensiolabs.org/

hek*_*mgl 8

检查以下代码:

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

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

输出:

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