pouncer Posted April 2, 2007 Share Posted April 2, 2007 function Save_Data($word) { $fp = fopen("search_strings.txt", "w"); fwrite($fp, $word); fclose($fp); } I'm trying to keep a list of keywords. Everytime i open the file to check it, it only has 1 word in it. it keeps overwriting the previous word. How do i kee a proper list of words? Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 2, 2007 Share Posted April 2, 2007 You need to open the file with "a+" format instead of "w": <?php function Save_Data($word) { $fp = fopen("search_strings.txt", "a+"); fwrite($fp, $word); fclose($fp); } ?> "a" places the pointer at the end of the file while "w" places at the beginning Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 2, 2007 Author Share Posted April 2, 2007 Thanks alot bud!! I got a few more problems now. If I call Save_Data("dog"); Save_Data("dog"); Save_Data("cat"); the text file appears: dogdogcat firstly, how can i stop it writing duplicates and secondly, how could i make the word appear in a list not on 1 line? Thanks guys! Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 2, 2007 Share Posted April 2, 2007 You need to write a newline character after each line yourself. PHP doesn't know that each write to the file needs to be on a new line unless you tell it. <?php function Save_Data($word) { $fp = fopen("search_strings.txt", "a+"); fwrite($fp, $word . "\r\n"); fclose($fp); } ?> Ken Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 2, 2007 Share Posted April 2, 2007 As for the dups, you'll need to run through the entire file first and see if the word exists. If not, insert it. Keep in mind that the larger your file gets, the longer it will take to parse. I recommend parsing it this way as opposed to using something like file() to avoid size issues later: <?php function Save_Data($word) { $save = TRUE; $fp = fopen("search_strings.txt", "r"); while (!feof($fp)) { $line = fgets($fp, 4096); $line = trim($line); if ($line == $word) $save = FALSE; } fclose($fp); if ($save) { $fp = fopen("search_strings.txt", "a+"); fwrite($fp, $word . "\r\n"); fclose($fp); } } ?> Hope this helps! Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 2, 2007 Author Share Posted April 2, 2007 Thanks alot obsedian and ken!! perfect!! and also i thought php would have its own function to be able to read a word from a text file so in future while the file gets bigger and bigger, would the while loop possibly slow things down? are there better ways you could suggest for me to save my words and be able to read from the file for duplicates? or is this present way the best way you think (also i don't really want to use sql tables to store my words) Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 2, 2007 Share Posted April 2, 2007 <?php // get contents of a file into a string $filename = "/usr/local/something.txt"; $handle = fopen($filename, "w+"); $contents = fread($handle, filesize($filename)); $contents .= $newword; fwrite($handle,$contents); fclose($handle); ?> Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 2, 2007 Author Share Posted April 2, 2007 wow. hey jitesh thanks for the snippet. will that also automatically check if the word already exists and deal with the duplicates? Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 2, 2007 Share Posted April 2, 2007 <?php // get contents of a file into a string $filename = "/usr/local/something.txt"; $handle = fopen($filename, "w+"); $contents = fread($handle, filesize($filename)); if(!strpos($contents,$newword)){ $contents .= $newword; } fwrite($handle,$contents); fclose($handle); ?> Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 2, 2007 Share Posted April 2, 2007 <?php // get contents of a file into a string $filename = "/usr/local/something.txt"; $handle = fopen($filename, "w+"); $contents = fread($handle, filesize($filename)); if(!strpos($contents,$newword)){ $contents .= $newword; } fwrite($handle,$contents); fclose($handle); ?> The only problem with handling the file like that is that you can very quickly get into the same problem I mentioned above (running into a memory issue by overloading a variable). When your file gets up to several MB in size, and you are attempting to assign the whole thing to a single variable (whether a string or array), you are quickly going to run into problems. Grant it, this will be quite a while, but you'll be better off to account for that now instead of having to redo everything later. You'll really be a lot safer if you handle each line of the file individually. Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 2, 2007 Author Share Posted April 2, 2007 hmmi see. ok obsidian in that case i will stick to your method!. Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 2, 2007 Author Share Posted April 2, 2007 What im actually using it for is this: <?php function Lookup_Word($word) { $host = "www.onelook.com"; $fp = fsockopen($host, 80, $errno, $errdesc) or die("Connection to $host failed"); $request = "GET /index.php3?w=" . $word . "&ls=a HTTP/1.0\r\n"; $request .= "Accept: */*\r\n"; $request .= "Referer: http://www.onelook.com/\r\n"; $request .= "Accept-Language: en-us\r\n"; $request .= "Host: www.onelook.com\r\n"; $request .= "Connection: Keep-Alive\r\n\r\n"; fputs($fp, $request); while(!feof($fp)){ $page[] = fgets($fp, 1024); } fclose($fp); for($i=0; $i<count($page); $i++){ if (strstr($page[$i], "<i>noun</i>") || strstr($page[i], "<i>adjective</i>")) { Save_Data($word); break; } } } function Save_Data($word) { $save = TRUE; $fp = fopen("search_strings.txt", "r"); while (!feof($fp)) { $line = fgets($fp, 4096); $line = trim($line); if ($line == $word) $save = FALSE; } fclose($fp); if ($save) { $fp = fopen("search_strings.txt", "a+"); fwrite($fp, $word . "\r\n"); fclose($fp); } } ?> but how can i tell whe the socket has finished reading data? so that i can then call another function on the text file Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.