$input = fopen('input.txt', 'r'); $output = fopen('output.txt', 'w'); while (!feof($input)) { $line = rtrim(fgets($output), "\r\n"); if (mb_strlen($line) !== 1) { fwrite($output, $line.PHP_EOL); 

The code should remove lines with 1 character, but displays an empty document.

  • Do you even understand what they wrote here? - madfan41k
  • This was recommended to me) - Nikolay Vasilenkov
  • one
    @ madfan41k, as I understand it, the code should remove lines from the text that are <= 1 character long - Let's say Pie
  • one
    @ Let'ssayPie how are you !== turned into <= ? :) - teran
  • 3
    Perhaps because you get fgets($output) instead of fgets($input) - user218976

2 answers 2

I would consider this option:

 $input = file('input.txt', FILE_IGNORE_NEW_LINES); $file = array_filter($input, function($value) { return strlen($value) !== 1; }); file_put_contents('output.txt', implode("\r\n", $file)); 

    You read from a file for writing, respectively, at the end and get an empty file.

     $input = fopen('input.txt', 'r'); $output = fopen('output.txt', 'w'); while (!feof($input)) { $line = rtrim(fgets($input), "\r\n"); if (mb_strlen($line) !== 1) { fwrite($output, $line.PHP_EOL);