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><name2o ccassionally. 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($quoteFi le)) {
$quotes .= fread($quoteFil e,1024);
}
}
}
$quotes = explode("\n", $quotes); 4 2838
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($qu otes, file($lpath . '/' . $f));
}
}
closedir($dir);
### Remove all blank entries from $quotes
$quotes = array_diff($quo tes, 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($qu otes);
?>
--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
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><name2o ccassionally. 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($quoteFi le)) {
$quotes .= fread($quoteFil e,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*******@attgl obal.net
=============== ===
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($qu otes, file($lpath . '/' . $f));
}
}
closedir($dir);
### Remove all blank entries from $quotes
$quotes = array_diff($quo tes, 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($qu otes);
?>
--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
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_asc ii_code = ord($quotes[0]);
--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 by the PHP gd2 Library.
The problem is that im trying to translate the 'fread' structure into a
MySQL blob reading, as follows:
|
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 this:
456 5 1.txt%&'
|
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)
|
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 i am, i explode the query string, and check $array
where i am for the main section.
Only now if there are two slashes behind each other
|
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 discern, fread
all the sudden kept returning the same byte over and over as if it were
no longer advancing in the file. I used a hex editor to determine the
address of the last byte read in the file. CF was the last address, D0
was not ever read...
| |
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
frequently(forwards and backwards) and do fread() from that offset.
Or better still, could anyone let me know some good ways to achieve
what I need to do as above?Can I get hold of the file and being able
to read in without using fread()? Using...
|
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 am trying to read in a file
and then parse it to assign the value to variables. I have read that
fread is more is better for this then fscanf, so I tried the following
code with the input file and resulting output:
|
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 = doGetTags($links,"START",$tag1,$tag2,$tag3,"END");
|
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 full analysis start.
My problem is that the FILE* I want do parse has been fopen'ed far away
from where I am and I don't know in which MODE my FILE* has been opened.
And additionally, my FILE* may not be a regular file, but a continuous
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |