2

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

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

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

4

1 回答 1

10

检查以下代码:

$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"
}
于 2013-04-27T01:51:50.930 回答