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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With