How to read comment blocks in PHP?

Check out Tokenizer.

To get all the comments in a file named test.php you’d do:

$tokens = token_get_all(file_get_contents("test.php"));
$comments = array();
foreach($tokens as $token) {
    if($token[0] == T_COMMENT || $token[0] == T_DOC_COMMENT) {
        $comments[] = $token[1];
    }
}
print_r($comments);

Leave a comment