Deleting and replacing data in .txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
implications
Forum Commoner
Posts: 25
Joined: Thu Apr 07, 2011 3:59 am

Deleting and replacing data in .txt file

Post 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?
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Deleting and replacing data in .txt file

Post 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]
implications
Forum Commoner
Posts: 25
Joined: Thu Apr 07, 2011 3:59 am

Re: Deleting and replacing data in .txt file

Post 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);
}
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Deleting and replacing data in .txt file

Post by aravona »

Why not use a database then? It'll be much easier to do this with MySQL.
implications
Forum Commoner
Posts: 25
Joined: Thu Apr 07, 2011 3:59 am

Re: Deleting and replacing data in .txt file

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Deleting and replacing data in .txt file

Post 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.
implications
Forum Commoner
Posts: 25
Joined: Thu Apr 07, 2011 3:59 am

Re: Deleting and replacing data in .txt file

Post 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?
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: Deleting and replacing data in .txt file

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