473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

fread, explode, and eof problem

Joe
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>, <quote1as it should, I get <name1>,
<quote1><name2occassionally. 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);

Oct 6 '06 #1
4 2816
Joe wrote:
and of course if their
is a [...] different [...] way of doing this I'm all ears
I'd use file() instead of fopen(), fread(), feof(), fclose().
I'd use arrays (I *love* arrays).

<?php
$quotes = array();
$dir = opendir($lpath);
while ($f = readdir($dir)) {
if (preg_match('/\.txt$/', $f)) {
$quotes = array_merge($quotes, file($lpath . '/' . $f));
}
}
closedir($dir);

### Remove all blank entries from $quotes
$quotes = array_diff($quotes, array("\n"));

###
### ATTENTION
### The $quotes array may have "holes"
### If you need the keys in order without "holes" uncomment the next line
// $quotes = array_merge($quotes);
?>
--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
Oct 7 '06 #2
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>, <quote1as it should, I get <name1>,
<quote1><name2occassionally. 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.
js*******@attglobal.net
==================
Oct 7 '06 #3
Joe
This code is a bit cleaner than what I was using, but I'm still having
a similar problem. Now, the first quote in each file has an extra
character before the authors name. I could probably use ltrim to get
rid of the extra character if I knew what it was. How do I find what
character it is? In the HTML source it just shows up as a rectangle.
Pedro Graca wrote:
Joe wrote:
and of course if their
is a [...] different [...] way of doing this I'm all ears

I'd use file() instead of fopen(), fread(), feof(), fclose().
I'd use arrays (I *love* arrays).

<?php
$quotes = array();
$dir = opendir($lpath);
while ($f = readdir($dir)) {
if (preg_match('/\.txt$/', $f)) {
$quotes = array_merge($quotes, file($lpath . '/' . $f));
}
}
closedir($dir);

### Remove all blank entries from $quotes
$quotes = array_diff($quotes, array("\n"));

###
### ATTENTION
### The $quotes array may have "holes"
### If you need the keys in order without "holes" uncomment the next line
// $quotes = array_merge($quotes);
?>
--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
Oct 16 '06 #4
Joe top-posted:
[...] Now, the first quote in each file has an extra
character before the authors name. I could probably use ltrim to get
rid of the extra character if I knew what it was.
Can you post an example input file?
How do I find what character it is?
$first_char = substr($quotes[0], 0, 1);
$first_char_ascii_code = ord($quotes[0]);

--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
Oct 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RootShell | last post by:
Dear All Im having some dificulty here: I found a great PHP code by Catalin Mihaila that reads a SRC (Sinclair Spectrum $SCREEN Image Format) and tranforms it into PNG format to show onscreen...
10
by: Alain Lafon | last post by:
Helas, I got something that should be a minor problem, but anyhow it isn't to me right now. A little code fragment: fread(&file_qn, x, 1, fp_q); The corresponding text file looks like...
4
by: janssenssimon | last post by:
//de structure om de highscores in op de slagen typedef struct score{ char *naam; int veld; int score; struct score *volg; }HIGH; void toonhighscores(void)
12
by: frizzle | last post by:
Hi there, i have a site with fake folders & files. htaccess rewrites everything to index.php?vars now in index.php i decide what file to include with a switch/case statement. to define where...
13
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
3
by: juleigha27 | last post by:
Hi, First off, I want to apologize if this already got posted it seemed like something happened when I tried to post it previously and it didn't work. I am new to file manipulation with c. I...
5
by: sathyashrayan | last post by:
Dear group, The function to be used as follows: $links = "http://www.campaignindia.in/feature/analysis"; $tag1 = '<div class=feature-wrapper>'; $tag2 = '<h1><a href'; $tag3 = "</a>"; $op =...
15
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.