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.*\r/";
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.
Try using http://ca3.php.net/manual/en/class.reflectionclass.php
and http://ca3.php.net/manual/en/class.reflectionfunction.php
This all You need to get function parameters.
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: