Вопрос:

fgets doesn't read empty lines and spaces properly

php

117 просмотра

1 ответ

113 Репутация автора

I'm having problems with this code:

$file = fopen("/home/user/Storage/something/else.java", "r");

while(!feof($file)) {
  echo fgets($file) . "\n";
}

fclose($file);

The output of it looks like this:

1
2
3

While the file is like this:

1

  2


 3

(It's just an example, of course the file doesn't look exactly like that)


So, my question is, why those spaces and blank lines aren't printed?

I need to display the full file, so if there's any other method it would help a lot

Автор: Diego Barreiro Источник Размещён: 08.11.2017 10:59

Ответы (1)


1 плюс

472214 Репутация автора

Решение

If you're outputting to an HTML page, whitespace is normally compressed. You can use the <pre> tag to display pre-formatted text, and it won't be changed.

$file = fopen("/home/user/Storage/something/else.java", "r");
echo '<pre>';
while(($line = fgets($file)) !== false) {
  echo $line . "\n";
}
echo '</pre>';
fclose($file);
Автор: Barmar Размещён: 09.11.2017 12:25
Вопросы из категории :
32x32