简体   繁体   中英

PHP how to read comments from twig file?

How can I read comments in twig files using PHP?

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);

Result:

Array ( )

Any ideas?

why not use regex?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM