PHP读取文件注释而不是文件内容-忘记了 [英] PHP read file comments NOT file content - forgotten
问题描述
对不起,人们忘记了这个,我需要阅读一个php文件示例中的第一个批处理"注释:
Sorry folks forgotten this one, I need to read the first "batch" of comment in a php file example would be:
<?php
/** This is some basic file info **/
?>
<?php This is the "file" proper" ?>
我需要阅读另一个文件中的第一条评论,但我完全忘记了如何获取/**这是一些基本文件信息**/作为字符串抱歉,但是要感谢
I need to read the first comment inside another file but I have totally forgotten how to get the /** This is some basic file info **/ as a string Sorry but thanks in adavance
推荐答案
有一个token_get_all($code)
函数可用于此功能,它比您最初想的要可靠.
There's a token_get_all($code)
function which can be used for this and it's more reliable than you first might think.
下面是一些示例代码,用于从文件中提取所有注释(未经测试,但足以使您入门):
Here's some example code to get all comments out of a file (it's untested, but should be enough to get you started):
<?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屋!