Re: Cannot output the same data from text file in PHP

From: Date: Thu, 14 May 2009 10:04:14 +0000
Subject: Re: Cannot output the same data from text file in PHP
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-292564@lists.php.net to get a copy of this message
You could even make it shorter, if you don't need the line numbers anyway: <pre> <? echo nl2br(file_get_contents('file.txt')); ?> </pre> 2009/5/14 Peter Ford <pete@justcroft.com>: > Moses wrote: >> Hi Folks, >> >> I have a written a script in PHP which outputs the result from a text file. >> The PHP script is as follows: >> >> <?php >> $alldata = file("result.txt"); >> echo "<table><tr><td>"; >> foreach($alldata as $line_num => $line) { >> echo $line."<br>"; >> } >> echo"</td></tr></table>"; >> ?> >> >> I have attached the result.txt file. However the output of the script is as >> follows: >> >> >> Query: 1 atggcaatcgtttcagcagaaaaattcgtaattcgagctcgcccggggatcgatcctcta 60 >> |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| >> Sbjct: 1 atggcaatcgtttcagcagaaaaattcgtaattcgagctcgcccggggatcgatcctcta 60 >> >> which is not exactly  as in the result.txt file in that the pipelines >> are displaced. >> >> Any pointer to this problem shall be appreciated. Thanking you in advance. >> >> Moses >> > > Not a PHP problem, but a HTML problem: > First, HTML compresses white space into just one space, so all of those leading > spaces on line 2 are lost. > Second, you are (probably) displaying using a proportionally-spaced font, so the > narrow pipeline characters take up less width than the letters. > > So you need something like: > <?php > $alldata = file("result.txt"); > echo "<table><tr><td style='white-space: pre; font-family: > monospace;'>"; > foreach($alldata as $line_num => $line) > { >    echo $line."\n"; > } > echo"</td></tr></table>"; > ?> > > > -- > Peter Ford                              phone: 01580 893333 > Developer                               fax:   01580 893399 > Justcroft International Ltd., Staplehurst, Kent > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >

« previous php.general (#292564) next »