Adam Pietrasiak
Adam Pietrasiak

Reputation: 13224

Php easy way to read comment data from file

Is there any easy way to read comment header from file (css/js/php)

like

/*
Script Name : somescript
Author : Me
Version : 1.1
*/

As simple key-value array?

Upvotes: 0

Views: 103

Answers (1)

Ing. Michal Hudak
Ing. Michal Hudak

Reputation: 5622

PHP:

Check out Tokenizer.

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

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

CSS:

jQuery.get("file.css", null, function(data) {
    var comments = data.match(/\/\*.*\*\//g);
    for each (var c in comments) 
        alert(c);
});

Upvotes: 2

Related Questions