[thelist] Displaying a txt file with PHP

jon steele jjsteele22 at yahoo.com
Sat Oct 6 15:06:23 CDT 2001


Hi Gustovo:

First, use fopen and fread to read the contents of the file:

$fp=fopen($file,'r');
$content=fread($fp,filesize($file));
fclose($fp);

Then, add a <p> to the beginning, and a </p> to the end, and use
str_replace to replace all char(10) (new lines) with </p><p>

$content = "<p>".$content."</p>";
$content = str_replace(chr(10),"</p><p>",$content);

Then output the variable:

echo $content;

Regarding using parsedfile.php to display parsedfile.txt, you would
need to do some server configuring for that. One idea is to have all
404 redirected to parse.php, which displays a text file based on the
referer.

Jon


--- Gustavo Arizpe <garizpe at network.com.mx> wrote:
> First of all, I'd like to thank Artlung Joe for his answer when I
> asked for
> a simple way to display CSV files with PHP. He suggested fgetcsv(),
> which is
> a PHP function that really rocks! And it's probably not so widely
> used, as I
> see MySQL used everywhere, even for very small projects.
> 
> Now I'd like to ask what is probably one of the most extended uses of
> PHP:
> How can I display a .txt file using PHP? Let's say I have a text
> file,
> filled with paragraphs of text.
> 
> How can I parse them using PHP, enveloping the paragraphs with
> <p>...</p>?
> The url would be something like
> http://mysite.com/file.php?parsedfile.txt
> 
> It would be even better to have the url like this
> http://mysite.com/parsedfile.php , where the php script would be
> reading
> parsedfile.txt, and generating the html from itself, without using
> arguments.
> 
> I know for sure that this is a very simple thing to do (like the csv
> thing),
> but that's probably the reason why I can't seem to find how to do it.
> All I
> find are are more or less complex content management systems that
> really are
> out what I'm trying to do, which is display the file, properly
> formatted.
> 
> Thanks so much,
> 
> 
> Gustavo
> http://www.area.com.mx
> 
> 
> ---------------------------------------
> For unsubscribe and other options, including
> the Tip Harvester and archive of TheList go to:
> http://lists.evolt.org Workers of the Web, evolt ! 


__________________________________________________
Do You Yahoo!?
NEW from Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
http://geocities.yahoo.com/ps/info1




More information about the thelist mailing list