Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

removing comments but not newlines

46 views
Skip to first unread message

Jivanmukta

unread,
Feb 18, 2019, 5:55:59 AM2/18/19
to
I have a question: how can I remove comments from PHP sourxe code
without removing newlines, I mean I want to have "\n". I need a solution
in PHP or bash. I cannot use php_strip_whitespace() because it removes
newlines.

Luuk

unread,
Feb 19, 2019, 3:00:43 AM2/19/19
to
It looks like example2 from this page does do that (not tested!)
http://php.net/manual/en/function.php-strip-whitespace.php


--
Luuk

Jivanmukta

unread,
Feb 19, 2019, 9:11:03 AM2/19/19
to
W dniu 18.02.2019 o 11:55, Jivanmukta pisze:
I found the solution:

<?php
$file = file_get_contents($argv[1]);
$commentTokens = array(T_COMMENT);
if (defined('T_DOC_COMMENT'))
$commentTokens[] = T_DOC_COMMENT; // PHP 5
if (defined('T_ML_COMMENT'))
$commentTokens[] = T_ML_COMMENT; // PHP 4
$tokens = token_get_all($file);
foreach ($tokens as $token) {
if (is_array($token)) {
if (in_array($token[0], $commentTokens)) continue;
$token = $token[1];
}
echo $token;
}

Thomas 'PointedEars' Lahn

unread,
Jun 11, 2019, 1:10:01 PM6/11/19
to
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.

kristja...@gmail.com

unread,
Mar 13, 2020, 3:00:49 PM3/13/20
to
Comment can also contain newline !


Kristjan
0 new messages