Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

working with file system 1

Status
Not open for further replies.

bccamp

Technical User
Jan 20, 2005
69
I'm trying to build an evolving list of words to be excluded from a master list. I can use:
Code:
$elimt=array("a", "and", "the");
if (!in_array($key,$elimt)){
allow words;
}

but when I try to have the words added to a file, words.txt, I can't get the same results. I add the words with:
Code:
$newwordadded="$newword, $elimt";
$fp="file.txt";
fwrite($fp,$newwordadded);

I've tried adding them with ' " ' and without. The text file that I wrote appears right with
Code:
"a", "and", "the"
or
Code:
a, and, the

I read the file with
Code:
$fp=open("file.txt");
$elimt=fgets($fp);
while (!feof($fp)){
   $elimt=$elimt.fgets($fp);
}
$elimt=(explode(",", $elimt));

but the code above will not remove the words like the first code will. What am I missing?
 
confused.

u r reading from a file and writing back the words to a file???

Known is handfull, Unknown is worldfull
 
I have a script that allows me to add words from the DB to a text file on my system. I'm trying to eliminate the words in the text file from the final DB file, so when I go through all the 400 files, I can eliminate words like "a, and, the" from a field in the DB, every field like 'KeyWords'. As for sleipnir214, if you mean the read or write modifier, it's there, I just left it out of my message.
Code:
$fp=open("file.txt","r");
and
Code:
$fp="file.txt","w";
. If that's not the case, I'm still not understanding.
 
>>have a script that allows me to add words from the DB to a text file on my system.

>>I'm trying to eliminate the words in the text file from the final DB file,

aint the above stmts contradicting???

Known is handfull, Unknown is worldfull
 
How are you writing the words to the file? One per line or one line with multiple words?

I would think, using one per line would be easiest, then you can use
Code:
$elimt = file('file.txt');
to read the file into an array.

Then use the in_array() function like you planned.

Ken
 
Sorry, I'll try to be more clear.

My DB has a field "KeyWords." In that field it has words like:
"computers, a, monitor, the, keyboard, and.

I have written a script that displays the KeyWords:
add computers
add a
add monitor

Where "add" is a link that adds the word to a text file. Another part of the program pulls all of the words from several fields in the DB and combines them to KeyWords. I'm trying to use the text file words as the "junk" that I want removed before KeyWords is written. So KeyWords will be have:
computers, monitor, keyboard

without "a, and, the"

That's why I'm reading and writing to the text file. I can write to it, and it looks fine. I can read from it, and display it in the browser and it looks fine. I just can't get (!in_array($key,$elimt)) to remove the words after reading them from the text file. I'm not getting them into the array correctly.
 
Thanks Ken,
I'm writing to the file in a single line, then using
Code:
$elimt=(explode("," $elimt))
after reading it back. Is that not putting it into an array?

with your suggestion, I would write:
Code:
fwrite($fp."/r/n".$newwordadded);
then instead of
Code:
$fp=open("file.txt");
$elimt=fgets($fp);
while (!feof($fp)){
   $elimt=$elimt.fgets($fp);
}
$elimt=(explode(",", $elimt));
I could use
Code:
$elimt=file($elimt.txt)
and that would read the whole file into the array? Would I need to enclose them quotes when writing?
 
To write each word on a single line
Code:
<?
$fp = fopen('file.txt','w');
fwrite($fp,implode("\r\n",$elimt)."\r\n");
fclose($fp);
?>
To read it back into an array
Code:
$elimt = file('file.txt');

Ken
 
ok, got a Bad Arguments on the
Code:
fwrite($fp,implode("\r\n",$elimt)."\r\n");
line, and it is erasing everything in that file. Don't I need to read whats in that file, combine it, then write it back to the file?
 
Thanks again Ken,
Had to leave and come back to it. Was getting frustrated. Final Code was slightly modified. Writing:
Code:
$fp1=fopen"elimt.txt","r";
$elimt=fread($fp1, filesize("elimt.txt"));
$newword=trim($KeyWord);
$new_elimt="$newword\t\n$elimt";
$fp2=fopen("elimt.txt", "w+");
fwrite($fp2, $new_elimt);
fclose($fp1);fclose($fp2);
unset($KeyWord[$a]);
Reading back I just needed to trim the data:
Code:
$elimt=file("elimt.txt");
sort($elimt);
$cnts=count($elimt);
for ($b=0;$b<$cnts;$b++) {
   $elimt[$b]=trim($elimt[$b]);
}

Thanks again for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top