Search code examples
phpstringiofgets

Read and print with \t in PHP


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

Solution

  • 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>';