• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1099
  • Last Modified:

Regular Expression to strip comments

Hello.

I want to remove ALL text in $text between

/*
      and
*/

What is the regular expression to do this?

Thanks!
0
hankknight
Asked:
hankknight
  • 3
  • 3
  • 2
  • +3
10 Solutions
 
kamermansCommented:
This might do the trick:

$text = preg_replace('#\/\*.*\*\/#', '', $text);
0
 
dr_dedoCommented:
kamermans's regex won't remove comments on multi lines
try this

$text = preg_replace("/\/\\*[\\s\\S]*?\\*\//", '', $text);
0
 
kamermansCommented:
really?
A: how is [\s\S]* different than .* ?
B: [\\s\\S] would match '\' or 's' or '\' or 'S' as far as I can tell
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dr_dedoCommented:
try this
<?
$word=
"/*this is a comment
more comment
even more
and still more comment */
";
$str1 = preg_replace('#\/\*.*\*\/#', '', $word);
echo $str1;
echo "<br>---------------------";
$str2 = preg_replace ("/\/\\*[\\s\\S]*?\\*\//", '', $word);
echo $str2;
?>
0
 
kamermansCommented:
I should stick to Perl for Perl Regular Expressions :P
0
 
KennyTMCommented:
kamermans's solution works if the /s flag is appended (to force . matches newline too). Also, the .* match needs to be ungreedy (i.e. use .*? or the /U flag) otherwise when there're 2 comments any normal text in between will be stripped off also.

$text = 'some text, /* some
comments, */ and more /* com
men
ts... */...';
$text = preg_replace('#/\*.*?\*/#s', '', $text);
echo $text;
0
 
RoonaanCommented:
In addition to KennyTM, you can simplify even further by using the ungreedy modifier:

$text = preg_replace('#/\*.*\*/#sU','',$text);

For single line comments you then can use:

$text = preg_replace('#//.*#','',$text);

For both regular expressions however you need to make sure that /* and // are not used inside any actual code. I think you already gathered that though.

-r-
0
 
ixtiCommented:
And what happens if, text would be like this:
<?php
$text_with_comments = <<<HEREDOC
<?php
/*
My multiline comments
with comments-in-comments
function foo() {
    // This is one-line comment
    /* this function
   had some multiline comments */
}
*/

// Another one-line comment
\$a = 'sample';
?>
HEREDOC;
?>

I guess in this case you have to use while loop:
<?php
// let's strip all single-line comments:
$text_with_comments = preg_replace("/\/\/.*/", "", $text_with_comments);

// now let's strip all multi-line comments
while (preg_match("/\/\*([^\/])*\*\//", $text_with_comments)) {
    $text_with_comments = preg_replace("/\/\*([^\/])*\*\//", "", $text_with_comments);
}

$text_with_comments = preg_replace("/(\r?\n)+/", "\r\n", $text_with_comments);

var_dump($text_with_comments);
?>
0
 
RoonaanCommented:
Then your code will not work when you run it anyhow. Should give an unexpected "}" error, or something.

-r-
0
 
KennyTMCommented:
Hi ixti.
In C-like languages, the multiline comments /* ... */ may not be nested. So I think the case isn't really a problem. Also, your code cannot strip comments such as /* 1/2 spoon of sugar. */
0
 
ixtiCommented:
Roonaan, why it should "give an unexpected "}" error, or something."?
In my example above result will be:
string(25) "<?php
$a = 'sample';
?>"
No errors, no notices...
0
 
ixtiCommented:
KennyTM, thanks I know it (about nested comments)... about striping comments like /* 1/2 */ I didn't thought... It really can't strip it.
0
 
mankod04Commented:
won't all of these fail on the (rare) case that a /* comment */ appears inside a set of quotes?

I found this just now (in perl), but have yet to decipher it entirely:  http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.27.html

#!/usr/local/bin/perl
$/ = undef;
$_ = <>;
s#//(.*)|/\*[^*]*\*+([^/*][^*]*\*+)*/|"(\\.|[^"\\])*"|'(\\.|[^'\\])*'|[^/"']+#  $1 ? "/*$1 */" : $& #ge;
print;

I have yet to figure out this part:  /\*[^*]*\*+([^/*][^*]*\*+)*/ for stripping out /* */ comments, and the last part   $1 ? "/*$1 */" : $&

I know it's in perl, but the syntax should be similar.  Any ideas?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now