That depends on what you consider a “comment”.
I presume that you want to reduce file size but keep the line numbers;
that’s a good idea in general, but I do not see why it would help with
PHP code.
FWIW:
This one removes single-line comments that start with “//” (PHP also
supports “#”, but you may want to keep shebangs), but keeps the line
(since “.” by default does not match newline):
$modified_code = preg_replace('#^\\s*//.*#', '', $code);
Removing multi-line comments *and* keeping the lines can be done but is not
trivial. This appears to work:
$modified_code = preg_replace_callback(
'#\\s*/\*(?:[^*]|\*[^/])*\*/#', /* multi-line comment */
function (array $matches) {
$comment_lines = preg_match_all('/\n/', $matches[0]);
return str_repeat('\n', $comment_lines);
},
$code);
<
https://php.net/preg_replace>
<
https://php.net/preg_match_all>
<
https://php.net/str_repeat>
The “bash” solution would require the use of another utility than bash if it
should be efficient (it can be done with while…read, but that is neither
efficient nor without other problems). I would use (GNU) sed(1) for single-
line comments; the expression above can be reused then, but “\\s” must be
replaced with “[[:space:]]”.
For more you should read the PHP manual and Jeffrey E. F. Friedl’s
“Mastering Regular Expressions”.
Also, this is Usenet: Please get a real name.
--
PointedEars
Zend Certified PHP Engineer <
http://www.zend.com/en/yellow-pages/ZEND024953>
<
https://github.com/PointedEars> | <
http://PointedEars.de/wsvn>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.