REMIYA
06-08-2005, 12:08 AM
I want to strip the single line and multi line comments wrapped in /* */.
The following RegEx does exatly that, but strips the comments also and in the strings.
//Strips all the multi-line and single-line /* */comments even in strings ""
$pattern ="/\/\\*[\\s\\S]*?\\*\//";
$replacement = "THE COMMENT HAS BEEN HERE";
echo nl2br(preg_replace($pattern, $replacement, $phpcode));
So an initial code of:
/*
This is a multiline comment
*/
/* This is a single line comment */
echo "Hi, /* This is a single line comment in a string*/";
Processed through the above RegEx returns:
THE COMMENT HAS BEEN HERE
THE COMMENT HAS BEEN HERE
echo "Hi, THE COMMENT HAS BEEN HERE";
I've done a lot of testing with RegExs, but all was in vain. So was the search through Internet.
Is there any solution to the problem using RegEx?
Please, test before you post.
The following RegEx does exatly that, but strips the comments also and in the strings.
//Strips all the multi-line and single-line /* */comments even in strings ""
$pattern ="/\/\\*[\\s\\S]*?\\*\//";
$replacement = "THE COMMENT HAS BEEN HERE";
echo nl2br(preg_replace($pattern, $replacement, $phpcode));
So an initial code of:
/*
This is a multiline comment
*/
/* This is a single line comment */
echo "Hi, /* This is a single line comment in a string*/";
Processed through the above RegEx returns:
THE COMMENT HAS BEEN HERE
THE COMMENT HAS BEEN HERE
echo "Hi, THE COMMENT HAS BEEN HERE";
I've done a lot of testing with RegExs, but all was in vain. So was the search through Internet.
Is there any solution to the problem using RegEx?
Please, test before you post.