阅读PHP格式的文件注释,而不是文件内容-忘记 [英] Read file comments in PHP, NOT file content - forgotten
本文介绍了阅读PHP格式的文件注释,而不是文件内容-忘记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要读取PHP文件中注释的第一批&q;。例如:
<?php
/** This is some basic file info **/
?>
<?php This is the "file" proper" ?>
我需要读取另一个文件中的第一个注释,但是如何将/** This is some basic file info **/
作为字符串获取?
推荐答案
有一个token_get_all($code)
函数可用于此操作,它比您想象的更可靠。
以下是从文件中获取所有注释的一些示例代码(未经测试,但应该足以让您入门):
<?php
$source = file_get_contents( "file.php" );
$tokens = token_get_all( $source );
$comment = array(
T_COMMENT, // All comments since PHP5
T_ML_COMMENT, // Multiline comments PHP4 only
T_DOC_COMMENT // PHPDoc comments
);
foreach( $tokens as $token ) {
if( !in_array($token[0], $comment) )
continue;
// Do something with the comment
$txt = $token[1];
}
?>
这篇关于阅读PHP格式的文件注释,而不是文件内容-忘记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文