卷曲大括号之间的Preg_match - preg_match

时间:2013-04-27 01:48:28

标签: php regex

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

array(
[1] => 'this' 
[2] => 'is' 
[3] => 'my'
[4] => 'example'
[5] => 'subject'

这在php模板引擎中很常见 引用聪明和树枝 http://www.smarty.net/ http://twig.sensiolabs.org/

1 个答案:

答案 0 :(得分:8)

检查以下代码:

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

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

输出:

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