Re: fread, explode, and eof problem



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
==================
.



Relevant Pages

  • Re: Case statement
    ... > user puts a space within quotes, ... > multiple arguments). ... If I want to catch alphanumeric strings and warn against spaces, ...
    (comp.unix.shell)
  • fread, explode, and eof problem
    ... I have a 'random quotes' plugin that I use which reads tab delimited ... quotes from multiple text files in a directory, ... to separate into an array of names and quotes. ...
    (comp.lang.php)
  • Re: New A/C: Help with determining fair replacement cost
    ... Most messages who imply/state that my quotes are too high do not tell ... are we talking multiple hundreds, ... Prev by Date: ...
    (alt.home.repair)
  • Re: How to replace a whole line (matching a pattern) in a text filke with SED ?
    ... Matt Benson wrote: ... >multiple lines) ... >Other characters can appear in these lines as well. ... >(without quotes). ...
    (comp.unix.shell)
  • Re: Spread table.
    ... > multiple files unless each file is on a separate drive array. ... > Raid 10 and with multiple processors Sql Server can use multiple threads to ...
    (microsoft.public.sqlserver.clients)

Loading