preg_匹配PHP注释,不包括引号中的if

preg_匹配PHP注释,不包括引号中的if,php,comments,preg-match,quotes,Php,Comments,Preg Match,Quotes,我正在尝试编写一个正则表达式来查找和匹配PHP代码文件中的注释文本,目前为止我所做的一切都很好,但有一个例外: 我的模式: $pattern='/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/'; 它仍然匹配这样的线条 $string="//this is not a comment" $string2="/*this is not a comment */" 我知道我需要添加一些地方(?:^ |[^”]+[^”]),但我真的不知道如何

我正在尝试编写一个正则表达式来查找和匹配PHP代码文件中的注释文本,目前为止我所做的一切都很好,但有一个例外:

我的模式:

$pattern='/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/';
它仍然匹配这样的线条

$string="//this is not a comment"

$string2="/*this is not a comment */"

我知道我需要添加一些地方(?:^ |[^”]+[^”]),但我真的不知道如何添加,甚至可以避免介于“”之间的任何内容吗?

正如您已经看到的,使用正则表达式将是一件棘手的事情。但是PHP内置了解析自身的函数,比如

下面是一个简单的测试脚本,它将读取名为
foo.PHP
的假设文件中的PHP代码,并打印出所有注释,而不考虑注释字符(
/
#
/***/
):


<?php
$code = file_get_contents('foo.php');
$tokens = token_get_all($code);
foreach ($tokens as $token) {
    if (is_array($token)) { // Sometimes the token element will be a single character, like ; . > ! etc.
        if (token_name($token[0]) === 'T_COMMENT' || token_name($token[0]) === 'T_DOC_COMMENT') {
            echo $token[1] . PHP_EOL;           
        }
    }
}