0

I'm opening a TXT file in PHP and want to read the lines of it one by one.

I'm using fgets() .

The problem is that the output is ignoring the tabulations ("\t") in the original file, which is not what I want.

There is some way to force PHP to don't ignore 'em?

My code:

$file = fopen("file.txt", "r") or die("<br><br>Error.");
while (!feof($file)) {
    $string = fgets($file, 4096);
    echo "<br> " . $string;
}
Luiz
  • 2,429
  • 8
  • 28
  • 43
  • use something like `preg_replace` or `str_replace` to turn the `\t` into something that's more useful to you, such as (4 spaces) – Martin Jul 19 '16 at 19:52
  • 2
    php doesn't change or ignore the data. The problem must be somewhere else. – Evert Jul 19 '16 at 19:52
  • 2
    PHP is **NOT** ignoring anything, your web browser is. Press `Ctrl + u` to see the source code. – MonkeyZeus Jul 19 '16 at 19:52
  • 1
    Possible duplicate of [Multiple Spaces Between Words in HTML without  ](http://stackoverflow.com/questions/4503001/multiple-spaces-between-words-in-html-without-nbsp) or http://stackoverflow.com/questions/1571648/html-tab-space-instead-of-multiple-non-breaking-spaces-nbsp – chris85 Jul 19 '16 at 19:53

1 Answers1

1

Your (probably everyone's for that matter) web browser is ignoring the tabs.

Try this:

$file = fopen("file.txt", "r") or die("<br><br>Error.");
echo '<pre>'
while (!feof($file)) {
    $string = fgets($file, 4096);
    echo "\n" . htmlentities($string);
}
echo '</pre>';

or

$file = fopen("file.txt", "r") or die("<br><br>Error.");
echo '<textarea>'
while (!feof($file)) {
    $string = fgets($file, 4096);
    echo "\n" . htmlentities($string);
}
echo '</textarea>';
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77