Jump to content

write to text file problem


pouncer

Recommended Posts

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

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!

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

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!

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)

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

?>

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.