Re: fread, explode, and eof problem
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Fri, 06 Oct 2006 21:50:49 -0400
Joe wrote:
I have a 'random quotes' plugin that I use which reads tab delimited
quotes from multiple text files in a directory, and then randomly
displays one. Each text file contains multiple lines, each listing a
person and a quote, separated by a tab, and each file is based around a
topic. I use fopen and fread to suck in all the text, and then explode
to separate into an array of names and quotes. It is working fine with
one small exception. When I run explode, the very last line of one file
ends up merged with the first line of the next file, presumably because
it isn't sensing the end of the last line in a file. So instead of
displaying <name1>, <quote1> as it should, I get <name1>,
<quote1><name2> occassionally. I'm currently working around this by
including a blank line at the end of each text file, and then filtering
out any random hits on that blank line, but I know their is a better
way (I just don't know what it is, I'm fairly new to PHP). Below is
the code I'm using to generate the array, does anyone have any
suggestions on how to get around this problem (and of course if their
is a completely different but better way of doing this I'm all ears)?
$dir = opendir($lpath);
while ($f = readdir($dir)) {
if (eregi("\.txt",$f)){
$quoteFile = fopen($lpath.'/'.$f, "r");
while (!feof($quoteFile)) {
$quotes .= fread($quoteFile,1024);
} }
}
$quotes = explode("\n", $quotes);
You're assuming the last line is terminated by a "\n" character. That's not necessarily true.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- References:
- fread, explode, and eof problem
- From: Joe
- fread, explode, and eof problem
- Prev by Date: Re: Creating an image - speed issues
- Next by Date: Re: include won't work for some reason
- Previous by thread: Re: fread, explode, and eof problem
- Next by thread: include won't work for some reason
- Index(es):
Relevant Pages
|
Loading