Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 798
ajay Avatar asked Dec 12 '22 14:12

ajay


1 Answers

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.

like image 197
nickf Avatar answered Jan 13 '23 14:01

nickf