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


Groups > comp.lang.php > #17871 > unrolled thread

removing comments but not newlines

Started byJivanmukta <jivanmukkta@poczta.onet.pl>
First post2019-02-18 11:55 +0100
Last post2020-03-13 12:00 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  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

#17871 — removing comments but not newlines

FromJivanmukta <jivanmukkta@poczta.onet.pl>
Date2019-02-18 11:55 +0100
Subjectremoving comments but not newlines
Message-ID<q4e2vr$323uk$1@portraits.wsisiz.edu.pl>
  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.

[toc] | [next] | [standalone]


#17872

FromLuuk <luuk@invalid.lan>
Date2019-02-19 09:00 +0100
Message-ID<5c6bb7a6$0$22337$e4fe514c@news.xs4all.nl>
In reply to#17871
On 18-2-2019 11:55, 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.


It looks like example2 from this page does do that (not tested!)
http://php.net/manual/en/function.php-strip-whitespace.php


-- 
Luuk

[toc] | [prev] | [next] | [standalone]


#17873

FromJivanmukta <jivanmukkta@poczta.onet.pl>
Date2019-02-19 15:10 +0100
Message-ID<q4h2pi$3k2ha$1@portraits.wsisiz.edu.pl>
In reply to#17871
W dniu 18.02.2019 o 11:55, Jivanmukta pisze:
>   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.

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;
}

[toc] | [prev] | [next] | [standalone]


#17971

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2019-06-11 19:09 +0200
Message-ID<2115903.ElGaqSPkdT@PointedEars.de>
In reply to#17871
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.

[toc] | [prev] | [next] | [standalone]


#18237

Fromkristjanise129@gmail.com
Date2020-03-13 12:00 -0700
Message-ID<89dc62b0-b1fd-4dd7-a347-0c76d5a149ef@googlegroups.com>
In reply to#17871
esmaspäev, 18. veebruar 2019 12:55.59 UTC+2 kirjutas Jivanmukta:
> 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.

Comment can also contain newline !


Kristjan

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web