PHP 如何从 twig 文件中读取注释?

PHP how to read comments from twig file?

如何使用 PHP 阅读 twig 文件 中的评论?

test.twig:

{# Holy cow Twig is awesome! #}

PHP:

$tokens = token_get_all(file_get_contents("test.twig"));
$comments = array();
foreach($tokens as $token) {
    if($token[0] == T_COMMENT || $token[0] == T_DOC_COMMENT) {
        $comments[] = $token[1];
    }
}
print_r($comments);

结果:

Array ( )

有什么想法吗?

为什么不使用正则表达式?

$txt = file_get_contents("test.twig");
if ( preg_match_all ("/{#([^}]*)#}/", $txt, $matches) )
  print_r($matches[1]);