从 PHP 代码中自动删除注释的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/503871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:55:33  来源:igfitidea点击:

Best way to automatically remove comments from PHP code

phpcommentsstrip

提问by benlumley

Whats the best way to remove comments from a PHP file?

从 PHP 文件中删除注释的最佳方法是什么?

I want to do something similar to strip-whitespace() - but it shouldn't remove the line breaks as well.

我想做一些类似于 strip-whitespace() 的事情 - 但它也不应该删除换行符。

EG:

例如:

I want this:

我要这个:

<?PHP
// something
if ($whatsit) {
    do_something(); # we do something here
    echo '<html>Some embedded HTML</html>';
}
/* another long 
comment
*/
some_more_code();
?>

to become:

成为:

<?PHP
if ($whatsit) {
    do_something();
    echo '<html>Some embedded HTML</html>';
}
some_more_code();
?>

(Although if the empty lines remain where comments are removed, that wouldn't be ok).

(虽然如果空行保留在注释被删除的地方,那是不行的)。

It may not be possible, because of the requirement to preserve embedded html - thats whats tripped up the things that have come up on google.

这可能是不可能的,因为需要保留嵌入的 html - 这就是谷歌上出现的问题。

回答by Ionu? G. Stan

I'd use tokenizer. Here's my solution. It should work on both PHP 4 and 5:

我会使用tokenizer。这是我的解决方案。它应该适用于 PHP 4 和 5:

$fileStr = file_get_contents('path/to/file');
$newStr  = '';

$commentTokens = array(T_COMMENT);

if (defined('T_DOC_COMMENT'))
    $commentTokens[] = T_DOC_COMMENT; // PHP 5
if (defined('T_ML_COMMENT'))
    $commentTokens[] = T_ML_COMMENT;  // PHP 4

$tokens = token_get_all($fileStr);

foreach ($tokens as $token) {    
    if (is_array($token)) {
        if (in_array($token[0], $commentTokens))
            continue;

        $token = $token[1];
    }

    $newStr .= $token;
}

echo $newStr;

回答by Paul Dixon

How about using php -w to generate a file stripped of comments and whitespace, then using a beautifier like PHP_Beautifierto reformat for readability?

如何使用 php -w 生成一个去掉注释和空格的文件,然后使用像PHP_Beautifier这样的美化重新格式化以提高可读性?

回答by John Tyler

Here's the function posted above, modified to recursively remove all comments from all php files within a directory and all its subdirectories:

这是上面发布的函数,修改为递归删除目录及其所有子目录中的所有 php 文件中的所有注释:

function rmcomments($id) {
    if (file_exists($id)) {
        if (is_dir($id)) {
            $handle = opendir($id);
            while($file = readdir($handle)) {
                if (($file != ".") && ($file != "..")) {
                    rmcomments($id."/".$file); }}
            closedir($handle); }
        else if ((is_file($id)) && (end(explode('.', $id)) == "php")) {
            if (!is_writable($id)) { chmod($id,0777); }
            if (is_writable($id)) {
                $fileStr = file_get_contents($id);
                $newStr  = '';
                $commentTokens = array(T_COMMENT);
                if (defined('T_DOC_COMMENT')) { $commentTokens[] = T_DOC_COMMENT; }
                if (defined('T_ML_COMMENT')) { $commentTokens[] = T_ML_COMMENT; }
                $tokens = token_get_all($fileStr);
                foreach ($tokens as $token) {    
                    if (is_array($token)) {
                        if (in_array($token[0], $commentTokens)) { continue; }
                        $token = $token[1]; }
                    $newStr .= $token; }
                if (!file_put_contents($id,$newStr)) {
                    $open = fopen($id,"w");
                    fwrite($open,$newStr);
                    fclose($open); }}}}}

rmcomments("path/to/directory");

回答by Tom Haigh

$fileStr = file_get_contents('file.php');
foreach (token_get_all($fileStr) as $token ) {
    if ($token[0] != T_COMMENT) {
        continue;
    }
    $fileStr = str_replace($token[1], '', $fileStr);
}

echo $fileStr;

editI realised Ionut G. Stan has already suggested this, but I will leave the example here

编辑我意识到 Ionut G. Stan 已经提出了这个建议,但我会在这里留下这个例子

回答by ZhiJia Tang

a version more powerful : remove all comments in the folder

一个更强大的版本:删除文件夹中的所有评论

<?php
$di = new RecursiveDirectoryIterator(__DIR__,RecursiveDirectoryIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($di);
$fileArr = [];
foreach($it as $file){
    if(pathinfo($file,PATHINFO_EXTENSION) == "php"){
        ob_start();
        echo $file;
        $file = ob_get_clean();
        $fileArr[] = $file;
    }
}
$arr = [T_COMMENT,T_DOC_COMMENT];
$count = count($fileArr);
for($i=1;$i < $count;$i++){
    $fileStr = file_get_contents($fileArr[$i]);
    foreach(token_get_all($fileStr) as $token){
        if(in_array($token[0],$arr)){
            $fileStr = str_replace($token[1],'',$fileStr);
        }            
    }
    file_put_contents($fileArr[$i],$fileStr);
}

回答by Pawel Dubiel

Bash solution: If you want to remove recursively comments from all PHP files starting from the current directory you can write in terminal this one-liner. ( it uses temp1file to store PHP content for processing ) Note that this will strip all white spaces with comments.

Bash 解决方案:如果您想从当前目录开始的所有 PHP 文件中递归删除注释,您可以在终端中编写此单行。(它使用temp1file 来存储 PHP 内容以进行处理) 请注意,这将删除带有注释的所有空格。

 find . -type f -name '*.php' | while read VAR; do php -wq $VAR > temp1  ;  cat temp1 > $VAR; done

Then you should remove temp1file after.

然后你应该删除temp1文件。

if PHP_BEAUTIFERis installed then you can get nicely formatted code without commentswith

如果PHP_BEAUTIFER安装,那么你可以得到很好的格式化代码,而无需注释

 find . -type f -name '*.php' | while read VAR; do php -wq $VAR > temp1; php_beautifier temp1 > temp2;  cat temp2 > $VAR; done;

then remove two files ( temp1, temp2)

然后删除两个文件 ( temp1, temp2)

回答by Marco Demaio

If you already use an editor like UltraEdit, you can open one or multiple PHP file/s and then use a simple Find&Replace (CTRL+R)with the following Perl regexp

如果你已经使用过像UltraEdit这样的编辑器,你可以打开一个或多个 PHP 文件,然后使用一个简单的 Find&Replace (CTRL+R)和下面的 Perl regexp

(?s)/\*.*\*/

Beware the above regexp removes also comments inside a sring, i.e. in echo "hello/*babe*/";the /*babe*/would be removed too. Hence, it could be a solution if you have few files to remove comments, in order to be absolutely sure it does not wrongly replace something that is not a comment you would have to run the Find&Replace command and approve each time what is getting replaced.

谨防上述正则表达式中移除了也评论一个SRING内,即echo "hello/*babe*/";/*babe*/将太删除。因此,如果您只有很少的文件要删除评论,这可能是一个解决方案,为了绝对确保它不会错误地替换不是评论的内容,您必须运行 Find&Replace 命令并在每次替换内容时进行批准。

回答by Steely Wing

/*
* T_ML_COMMENT does not exist in PHP 5.
* The following three lines define it in order to
* preserve backwards compatibility.
*
* The next two lines define the PHP 5 only T_DOC_COMMENT,
* which we will mask as T_ML_COMMENT for PHP 4.
*/

if (! defined('T_ML_COMMENT')) {
    define('T_ML_COMMENT', T_COMMENT);
} else {
    define('T_DOC_COMMENT', T_ML_COMMENT);
}

/*
 * Remove all comment in $file
 */

function remove_comment($file) {
    $comment_token = array(T_COMMENT, T_ML_COMMENT, T_DOC_COMMENT);

    $input = file_get_contents($file);
    $tokens = token_get_all($input);
    $output = '';

    foreach ($tokens as $token) {
        if (is_string($token)) {
            $output .= $token;
        } else {
            list($id, $text) = $token;

            if (in_array($id, $comment_token)) {
                $output .= $text;
            }
        }
    }

    file_put_contents($file, $output);
}

/*
 * Glob recursive
 * @return ['dir/filename', ...]
 */

function glob_recursive($pattern, $flags = 0) {
    $file_list = glob($pattern, $flags);

    $sub_dir = glob(dirname($pattern) . '/*', GLOB_ONLYDIR);
    // If sub directory exist
    if (count($sub_dir) > 0) {
        $file_list = array_merge(
            glob_recursive(dirname($pattern) . '/*/' . basename($pattern), $flags),
            $file_list
        );
    }

    return $file_list;
}

// Remove all comment of '*.php', include sub directory
foreach (glob_recursive('*.php') as $file) {
    remove_comment($file);
}

回答by Deele

For ajax/json responses, I use following PHP code, to remove comments from HTML/JavaScript code, so it would be smaller (about 15% gain for my code).

对于 ajax/json 响应,我使用以下 PHP 代码从 HTML/JavaScript 代码中删除注释,因此它会更小(我的代码增益约为 15%)。

// Replace doubled spaces with single ones (ignored in HTML any way)
$html = preg_replace('@(\s){2,}@', '', $html);
// Remove single and multiline comments, tabs and newline chars
$html = preg_replace(
    '@(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|((?<!:)//.*)|[\t\r\n]@i',
    '',
    $html
);

Short and effective, but can produce unexpected results, if your code has $itty syntax.

简短而有效,但如果您的代码具有 $itty 语法,则会产生意想不到的结果。

回答by Robi Parvez

Run the command php --strip file.phpin a command prompt (i.e. cmd.exe), then browse to http://www.writephponline.com/phpbeautifier.

php --strip file.php在命令提示符(即cmd.exe)中运行命令,然后浏览到http://www.writephponline.com/phpbeautifier

Here, file.phpis your own file.

在这里,file.php是您自己的文件。

1

1