Jump to content

write to text file problem


pouncer

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

<?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);

?>

Link to comment
Share on other sites

<?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);

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.