Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #17971

Re: removing comments but not newlines

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: removing comments but not newlines
Date 2019-06-11 19:09 +0200
Organization PointedEars Software (PES)
Message-ID <2115903.ElGaqSPkdT@PointedEars.de> (permalink)
References <q4e2vr$323uk$1@portraits.wsisiz.edu.pl>

Show all headers | View raw


Jivanmukta wrote:

>   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.

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.

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

removing comments but not newlines Jivanmukta <jivanmukkta@poczta.onet.pl> - 2019-02-18 11:55 +0100
  Re: removing comments but not newlines Luuk <luuk@invalid.lan> - 2019-02-19 09:00 +0100
  Re: removing comments but not newlines Jivanmukta <jivanmukkta@poczta.onet.pl> - 2019-02-19 15:10 +0100
  Re: removing comments but not newlines Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-11 19:09 +0200
  Re: removing comments but not newlines kristjanise129@gmail.com - 2020-03-13 12:00 -0700

csiph-web