Page 1 of 1

Deleting and replacing data in .txt file

Posted: Thu May 12, 2011 10:23 am
by implications
I have created this PHP script where users can submit data through a form to a .txt file format database. Now I want to create a function where you can delete an existing row of text and replace it with a new row of text. How would I achieve something like this?

Re: Deleting and replacing data in .txt file

Posted: Thu May 12, 2011 10:30 am
by aravona
You can use code in php to just write again.

For example, you have a stored array in a text file and the variable is stored as 0.

If you write the varaible into the same text file it will update it. Just overwriting it.

Is this the kind of thing you mean?

Code: Select all

$store = array( 
            'storedarray' => '0',  
        ); 
         
$fp = fopen('info.txt','w'); 

fwrite($fp,serialize($store)); 


[text]a:1:{s:13:"storedarray";s:1:"0";}[/text]

Code: Select all

$store = array( 
            'storedarray' => '1',  
        ); 
         
$fp = fopen('info.txt','w'); 

fwrite($fp,serialize($store)); 


[text]a:1:{s:13:"storedarray";s:1:"1";}[/text]

Re: Deleting and replacing data in .txt file

Posted: Sun May 15, 2011 1:20 am
by implications
It's a little more complex because the data is user inputted and stored under an 'ID' in the text file. It goes something like this:

Code: Select all

1\¦Data\¦IP Address\¦
2\¦Data\¦IP Address\¦
3\¦Data\¦IP Address\¦
Say if a person wants to delete the second line and replace it with something else, then the function would locate the second line, delete it and replace it with something else.

I have tried creating a function but I'm new to this whole PHP thing so it might be a little flawed/redundant. I just gathered some functions from the PHP manual and compiled it but it doesn't seem to work.

Code: Select all

function replaceRow($database, $matchid, $wdata) {
	$wdata = trim($wdata);
	$newdata = str_replace($matchid, $wdata, $row);
	file_put_contents($database, $newdata, FILE_APPEND | LOCK_EX);
}

Re: Deleting and replacing data in .txt file

Posted: Mon May 16, 2011 5:15 am
by aravona
Why not use a database then? It'll be much easier to do this with MySQL.

Re: Deleting and replacing data in .txt file

Posted: Tue May 17, 2011 9:38 am
by implications
I would use MySQL but I'm trying to create a flatfile script because this is a pretty small script that doesn't require a large scale database like MySQL. Plus I've never used MySQL to build scripts before so I want to familiarize myself in a PHP setting before dabbling with MySQL.

But I will try and implement this with MySQL if using .txt files is impossible for the function I'm trying to achieve. I'm just trying to see if there's any way I can do this.

Re: Deleting and replacing data in .txt file

Posted: Tue May 17, 2011 10:41 am
by flying_circus
implications wrote:It's a little more complex because the data is user inputted and stored under an 'ID' in the text file. It goes something like this:

Code: Select all

1\¦Data\¦IP Address\¦
2\¦Data\¦IP Address\¦
3\¦Data\¦IP Address\¦
Say if a person wants to delete the second line and replace it with something else, then the function would locate the second line, delete it and replace it with something else.

I have tried creating a function but I'm new to this whole PHP thing so it might be a little flawed/redundant. I just gathered some functions from the PHP manual and compiled it but it doesn't seem to work.

Code: Select all

function replaceRow($database, $matchid, $wdata) {
	$wdata = trim($wdata);
	$newdata = str_replace($matchid, $wdata, $row);
	file_put_contents($database, $newdata, FILE_APPEND | LOCK_EX);
}
file_put_contents will either overwrite a complete file or append data to the end. If you are going to use that function, on each page request you must read the entire text file into an array, make your changes, then output the file as a whole.

If you want to go line by line, I think you need to concentrate on the file pointer. You should be able to use fgets() to read a file line by line in a while loop, and if the line matches your criteria for modifcation, use ftell() to get the pointer, use fseek() to set the pointer of of the file handle, and then use fwrite() to overwrite the data in the text file.

I have not personally spent any time working with a flat file database like you are building, but if I were to give it a go, the above is where I would start. Sorry I can't help more than speculation.

Re: Deleting and replacing data in .txt file

Posted: Sat May 21, 2011 10:04 pm
by implications
How can I use fgets to find the line that needs to be replaced by ID? The PHP manual says you fgets reads lines by file pointer and the file pointers are handle and length. Would it be able to do something like find the $matchid then replace the line that the $matchid is in or something along those lines?

Re: Deleting and replacing data in .txt file

Posted: Mon May 23, 2011 1:08 pm
by litebearer
Take a look at this, it may help...

Code: Select all

<?PHP
$my_data = "data.txt";
/* put the file contents into an array */
$lines = file_get_contents($my_data);
/* each line/record should have some value that is unique to that record */
$unique_id ="some value unique to line";
/* the replacement value(s) */
$new_line ="value to replace old line";
/* loop thru the array and find the element to be replaced */
for($i=0,$i<count($lines);$i++) {
	if(!stristr($lines[$i],$unique_id)) {
	}else{
		$lines[$i] = $new_line;
		break;
	}
}
/* store the data back to tthe file */
$data = implode("\n", $lines);
$file_put_contents($my_data, $data);
?>