Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx to remove /** */ and // ** **// php comments

ReGex newbie here.

I need to remove two different style comments from PHP files using RegEx.

I've found this expression to run in the BBEdit text editor:

\/\*[\s\S]*?\*\/

and it removes comments in the first style, like below:

/** This comment gets removed with my regex */

But it doesn't remove these style comments:

// ** This comment has the double leadng-trailng slashes ** //

I don't know why there is a mix of the two different types of comments, and there are only a few of the // comments, but I need to delete them all.

Adding another slash to the search, i.e.

\/\\*[\s\S]*?\*\/

makes the expression greedy and it removes single slashes in non-commented code. A working expression will require obviously more complexity than that :)

like image 756
markratledge Avatar asked Feb 12 '23 03:02

markratledge


1 Answers

PHP has got a built-in function for that:

php_strip_whitespace($filename);
like image 184
jeroen Avatar answered Feb 15 '23 11:02

jeroen