在php文件中获取注释 [英] Get comments in a php file

查看:300
本文介绍了在php文件中获取注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从我的服务器上的某个 .php 文件中获取注释,以便解析它的变量。我认为Ii找到了一个简单的方法来做到这一点,但是,我使用的函数不返回任何东西,即使我明确有文件中的意见。

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.

这里是我使用的注释:

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

这是我的代码:

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.

编辑:

我发现了一个答案:

首先,我应该在我的问题中注意到,我试图写一个系统读取插件。这些插件文件应该在顶部包含一个注释块,包含插件作者,网站,电子邮件等变量。

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.

所以这里是我做的:

我使用feeela的例子来改变我的函数来获取注释和它的变量。

然后我改变了它的代码,以适应我的需要:

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);
}



我把代码的结果通过正则表达式匹配给我,

然后我使用split函数实际上给我分隔的参数和值,所以我可以将它们返回到调用函数的变量。

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
*/

/*
@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.

在某些人眼中,这可能不是那么高效。正如feeela所说,如果你的评论风格改变了怎么办?我将是唯一一个在这个项目上工作和编写插件的人。因此,对于我来说,保持注释风格在每个插件脚本中都不会太难。

此方法对我来说非常合适。

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.

感谢大家对此的帮助。

推荐答案

您可以使用 token_get_all(),它使用Zend引擎的词法扫描器将给定的PHP源字符串解析为语言令牌。

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

请参阅: http://php.net/token_get_all

下面是一个示例函数,我使用一次从当前文件获取file-doc注释:

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];
}

这篇关于在php文件中获取注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆