-1

I need to delete a particular file content that is between these two characters /* & */ using PHP. The file from which I am trying to remove these comments is very large and includes a large data set, So optimized solution will be appreciated.

Example content:

/*SOME TEXT HERE

*/ 12314

So, the final file should contain only

1234

Here is the method that keeps on running until we got the comments string. Please note that the comments are only at one place in the file and they are always on the top of the file. Please let me know how can I delete those lines on which match the comments condition? Below is the method that I updated.

 $reading = fopen(public_path('file.csv'), 'r');
$writing = fopen(public_path('file.csv'), 'w');
$counter = 0;
$line = "";
$no_of_lines = 0;


while (!feof($reading) && $counter != 2) {

    $new_line = fgets($reading);

    if ($matched_string = strstr($new_line, "/*")) {
        $line = $line . $matched_string;        
        $counter++;
    } elseif ($matched_string = strstr($new_line, "*/")) {
        $line = $line . $matched_string;
        $counter++;
    } else {
        $line = $line . $new_line;
        fwrite($writing, "");
    }
    $no_of_lines++;

}


fclose($writing);
fclose($reading);
Atif Javed
  • 123
  • 1
  • 2
  • 8

1 Answers1

0

First open the file but one line at a time to save memory:

<?php

$reading = fopen('myfile', 'r');
$writing = fopen('newfile', 'w');

while (!feof($reading)) {
  $line = fgets($reading);

  // We will put the removal logic in here      

  fputs($writing, $line);
}

fclose($reading); 
fclose($writing);

For the removal, use some regex.

<?php

$line = preg_replace('#\/\*.+\*\/#', '/* */', $line);

You can see this working here https://3v4l.org/XmltD

If you don't want the /* either, just change the replace call to this:

$string = preg_replace('#\/\*.+\*\/#', '', $string);
delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • I am trying to remove the comments from file not from a string, I have edited the discription again. – Atif Javed Sep 19 '18 at 10:37
  • Thats not a concern. When you `file_get_contents()`, it becomes a string, right? ;-) – delboy1978uk Sep 19 '18 at 10:38
  • http://php.net/manual/en/function.file-get-contents.php – delboy1978uk Sep 19 '18 at 10:39
  • Yes, it will. But as I have mentioned the file contains a large amount of data set, so it will not be an optimized solution. Thats why – Atif Javed Sep 19 '18 at 10:40
  • ok, updating my answer then – delboy1978uk Sep 19 '18 at 10:41
  • Thanks, for the code update. But what if the comment starting is on the first line and ending in on like 10th line? – Atif Javed Sep 19 '18 at 11:08
  • then this method wouldn't work. Maybe you could check for `/*` using `strstr()`, and concat each subsequent line until another `strstr()` finds the `*/`? Once you get the full string, then you can run the regex and continue – delboy1978uk Sep 19 '18 at 11:12
  • I just updated my question and added the method according to your suggested solution. I am now able to detect the comment lines, please let me know how can I keep deleting the lines from the file as I am detecting the comment lines. – Atif Javed Sep 19 '18 at 20:50