PHP读取文件注释不是文件内容 - 忘记了

use*_*657 4 php commenting

对不起人们忘记了这一个,我需要在php文件中读取第一批"评论"的例子:

<?php
/** This is some basic file info **/
?>
<?php This is the "file" proper" ?>
Run Code Online (Sandbox Code Playgroud)

我需要阅读另一个文件中的第一条评论,但我完全忘记了如何获取/**这是一些基本的文件信息**/作为一个字符串抱歉,但感谢adavance

sve*_*ens 14

有一个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];
    }

?>
Run Code Online (Sandbox Code Playgroud)

  • 嘿,我只会稍微改变一下......将`break`更改为`continue`,以便继续查找内容中的所有注释. (2认同)