Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

parsing - Get comments in a php file

I've been trying to get the comments out of a certain .php file on my server, in order to parse it's variables. I thought Ii found an easy way to do this, however, the function I use doesn't return anything, even though I clearly have comments in the file.

Here are the comments I use:

/**
* @param  foo bar
* @return baz
*/

Here's my code:

function GetComments($filename) {

    $expr = "/((?:/*(?:[^*]|(?:*+[^*/]))**+/)|(?://.*))/";

    $file = fopen($filename, "r");
    $length = filesize($filename);
    $comments = fread($file, $length);
    fclose($file);

    preg_match_all($expr, $comments, $matches);

    foreach($matches[0] as $id => $variable){
        $comments = str_replace($variable,'',$comments);
    }

    return $comments;
}

Is there something I'm doing wrong? Because if so, I'm clearly over looking it.

Any help would be very welcome.

EDIT:

I found an answer:

First of all, I should have probably noted in my question that I am trying to write a system for reading plugins. These plugin files should contain a comment block at the top containing variables like the plugin's author, website, email, etc.

so here's what i did:

I took feeela's example to change my function to get the comments and it's variables.
I then changed it's code a bit to fit my needs:

public function GetComments($filename)
{

    $docComments = array_filter(token_get_all(file_get_contents($filename)), function($entry)
    {
        return $entry[0] == T_COMMENT;
    });
    $fileDocComment = array_shift($docComments);

    $regexp = "/@.*:s.*
/";
    preg_match_all($regexp, $fileDocComment[1], $matches);

    for($i = 0; $i < sizeof($matches[0]); $i++)
    {
        $params[$i] = split(": ", $matches[0][$i]);
    }

    return($params);
}

I put the result of the code feeela gave me through a regular expression match, resulting in an array containing the parameters and their values combined.
Then I used the split function to actually give me seperated parameters and values, so I could return them to the variable that called the function.

In order to get this to work properly, I needed to change the comment style I used from

/**
* @param foo bar
* @return baz
*/

to

/*
@param: foo bar
@return: baz
*/

making it a normal comment block, instead of a doc comment block.
And it also enabled me to use ': ' as a pattern for the split function.

It might be 'not so efficient' in the eyes of some. As feeela noted, "what if your comment style changes?". I will be the only one working on this project and writing the plugins. Therefore it will not be too hard for me too keep the comment style the same in every plugin script.
This method works perfectly for me.

Thank you all for your help on this one.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use token_get_all(), which "parses a given PHP source string into language tokens using the Zend engine's lexical scanner".

See: http://php.net/token_get_all

Here's an example function I've used once to get the file-doc-comment from the current file:

/**
 * Return first doc comment found in this file.
 * 
 * @return string
 */
function getFileDocBlock()
{
    $docComments = array_filter(
        token_get_all( file_get_contents( __FILE__ ) ), function($entry) {
            return $entry[0] == T_DOC_COMMENT;
        }
    );
    $fileDocComment = array_shift( $docComments );
    return $fileDocComment[1];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...