How to remove single line comments in php (eg “// remove this comment”)?

孤街醉人 提交于 2019-12-06 05:43:39

问题


I want to remove all single line comments (eg //comments) from my code using regular expression.

By now I'm using: preg_replace('/\/\/(.*)/','',$html); but it also removes strings like http://example.com.


回答1:


You cannot do this reliably. There is no guarantee that // at any position in a file indicates a comment in PHP context. It might very well be contained in a string for example.

It's only possible to approach this with a few concessions. For example if it is sufficient if it catches // comments on a single line, then this would be an option with less false positives:

$source = preg_replace('#^\s*//.+$#m', "", $source);

The real solution would be utilize a language parser, but that's obviously overkill. So try with adding some heuristics to avoid removing wrong occourences.




回答2:


Perhaps a better method would be to use the PHP engine itself, perhaps by using token_get_all(). That function will tokenise a PHP script, so you can view it exactly as PHP views it, and hence remove or replace comments.

Doing this with a regex alone would be at best a nightmare, and most likely not possible at all.




回答3:


 preg_replace('/.*?:?(\/\/.*)/','',$html);

You could try something like this, but I'm sure you can safe use regular expressions to account for all possible edge cases.

However as mentioned above using a tokenizer is a better and more reliable method of doing this. In fact there is an example of how to remove comments from a PHP file in the comments on php.net man pages, see here. This could server as a good start point, but I recommended testing this for yourself. Code in the comments on php.net man pages can often be a bit dodgy.




回答4:


If you want to minify your PHP code, why not use php_strip_whitespace( )?




回答5:


If you don't get any other alternative, might I suggest. Although performance wise it's not the best approach.

$lines = explode("\n", $source);
$lines = array_map(
  function($line) {
    return preg_replace("@\s*//.*$@", '', $line);
  },
  $lines
);
$source = implode("\n", $lines);



回答6:


function stripPhpComments($code)
{
    $tokens = token_get_all($code);
    $strippedCode = '';

    while($token = array_shift($tokens)) {        
        if((is_array($token) && token_name($token[0]) !== 'T_COMMENT') 
            || !is_array($token)) 
        {
            $strippedCode .= is_array($token) ? $token[1] : $token;
        }
    }
    return $strippedCode;        
}


来源:https://stackoverflow.com/questions/5419154/how-to-remove-single-line-comments-in-php-eg-remove-this-comment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!