如何使用正则表达式捕获嵌套的{%if ...%} {%endif%}语句

Xes*_*sau -2 php regex preg-replace-callback

这就是我现在所得到的:

/{% if(.+?) %}(.*?){% endif %}/gusi

它会捕获多个if语句等等.

IMG:http://image.xesau.eu/2015-02-07_23-22-11.png

但是当我做嵌套的那些时,if if in if,它会在{%endif%}的第一次出现时停止

IMG:http://image.xesau.eu/2015-02-08_09-29-43.png

有没有办法像{%if ...%}语句那样捕获尽可能多的{%endif%}语句,如果有,怎么样?

dec*_*eze 5

不要使用regexen,请使用现有的Twig解析器.这是我编写的一个提取器的示例,它解析自定义标记并提取它们:https://github.com/deceze/Twig-extensions/tree/master/lib/Twig/Extensions/Extension/Gettext

词法分析器的工作是将Twig源代码转换为对象; 你可以扩展它,如果你需要挂钩到这个过程:

class My_Twig_Lexer extends Twig_Lexer {

    ...

    /**
     * Overrides lexComment by saving comment tokens into $this->commentTokens
     * instead of just ignoring them.
     */
    protected function lexComment() {
        if (!preg_match($this->regexes['lex_comment'], $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor)) {
            throw new Twig_Error_Syntax('Unclosed comment', $this->lineno, $this->filename);
        }
        $value = substr($this->code, $this->cursor, $match[0][1] - $this->cursor);
        $token = new Twig_Extensions_Extension_Gettext_Token(Twig_Extensions_Extension_Gettext_Token::COMMENT, $value, $this->lineno);
        $this->commentTokens[] = $token;
        $this->moveCursor($value . $match[0][0]);
    }

    ...

}
Run Code Online (Sandbox Code Playgroud)

通常Twig注释节点被Twig丢弃,这个词法分析器会保存它们.

但是,您主要关注的是使用解析器:

$twig   = new Twig_Environment(new Twig_Loader_String);
$lexer  = new My_Twig_Lexer($twig);
$parser = new Twig_Parser($twig);

$source = file_get_contents($file);
$tokens = $lexer->tokenize($source);
$node   = $parser->parse($tokens);
processNode($node);
Run Code Online (Sandbox Code Playgroud)

$node这是节点树的根节点,它以面向对象的方式表示T​​wig源,所有节点都已正确解析.您只需处理此树,而无需担心用于生成它的确切语法:

 processNode(Twig_NodeInterface $node) {
      switch (true) {
          case $node instanceof Twig_Node_Expression_Function :
              processFunctionNode($node);
              break;
          case $node instanceof Twig_Node_Expression_Filter :
              processFilterNode($node);
              break;
      }

      foreach ($node as $child) {
          if ($child instanceof Twig_NodeInterface) {
              processNode($child);
          }
      }
 }
Run Code Online (Sandbox Code Playgroud)

只需遍历它,直到找到您正在寻找的节点类型并获取其信息.玩一下吧.这个示例代码可能有点过时,也可能没有过时,你必须深入研究Twig解析器源代码才能理解它.