簡體   English   中英

PHP如何從樹枝文件中讀取注釋?

[英]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]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM