Jump to content

Recommended Posts

Hey Its me again with another question for eager answers. XD

Ok so now I wish to overwrite a particular line in a text document, HOWEVER I do not want to change the order of the lines in the text document... 

 

Here is a section of code I am currently using to determine the Line Number of the line I wish to replace

$cmdFilelookup = file($cmdFileName);
foreach ($cmdFilelookup as $line_num => $gamerfile) {
if ($gamerfile == $_POST['gamerfile']) {

 

Now I'm not sure where to go now. I want to change the contents of the entire line, but keep every other line in tact.

 

Any thoughts of what to do next or a differnt way to do it would be Greatly loved XD.

 

Link to comment
https://forums.phpfreaks.com/topic/121770-solved-overwritting-a-txt-line/
Share on other sites

I've done this method many times in a few pieces of coding I did (mainly for quizzes when I wanted to either modify choices or questions, etc...).

 

If you know exactly what line to replace every time, try the following:

 

<?php

$data = file($cmdFileName);//this reads the whole file into an array

$data[$linenumber] == "new stuff to put in this line";

$new_data = implode("\n",$data);//this will implode the array into one big text, fitted with line breaks.

$fp = fopen($cmdFileName,"w+");//this opens the file and clears it at the same time
fwrite($fp,$new_data);
fclose($fp);
?>

 

Add respective error handler functions as needed.

 

Here's your basic logic:

 

Put the whole file contents into an array (line by line). Find the line number (identical to the array index at this point) and set it to a different value. Implode the array into a single block of text with line breaks in order for it to be written to a file (as you can't write an array to a file!) Open up the file for writing, truncating it automatically. Write the new file contents, and close the handler.

Ok So I tried doing this, however it didn't write to the file or change it like it was supposed too.. Can I get a second pair of eyes to see what I did wrong? Thanks

 

$cmdFileName = "command.txt";
$cmdFilelookup = file($cmdFileName);
foreach ($cmdFilelookup as $line_num => $gamerfile) {
if (rtrim($gamerfile) == $_POST['gamerfile']) {
$replacewith = 'modules/command/' . $_POST["memberid"] . $_POST["rank"] . '.txt';
$data = file($cmdFileName);//this reads the whole file into an array
$data[$line_num] == $replacewith;
$new_data = implode("\n",$data);//this will implode the array into one big text, fitted with line breaks.
$fp = fopen($cmdFileName,"w+");//this opens the file and clears it at the same time
fwrite($fp,$new_data);
fclose($fp);
}
}

$cmdFileName = "command.txt";
$cmdFilelookup = file($cmdFileName);
foreach ($cmdFilelookup as $line_num => $gamerfile) {
if (rtrim($gamerfile) == $_POST['gamerfile']) {
$replacewith = 'modules/command/' . $_POST["memberid"] . $_POST["rank"] . '.txt';
$data = file($cmdFileName);//this reads the whole file into an array
$data[$line_num] = $replacewith;
$new_data = implode("\n",$data);//this will implode the array into one big text, fitted with line breaks.
$fp = fopen($cmdFileName,"w+");//this opens the file and clears it at the same time
fwrite($fp,$new_data);
fclose($fp);
}
}

 

Ok, what I'm confused is why you're going through the file.. twice. Exactly how are you determining the correct line to edit?

actually you have a point here.. why am I lol...

 

Ok so I fixed that part and edited it slightly to be somewhat less garbled

 

$cmdFileName = "command.txt";
$data = file($cmdFileName);
foreach ($data as $line_num => $gamerfile) {
if (rtrim($gamerfile) == $_POST['gamerfile']) {
$replacewith = 'modules/command/' . $_POST["memberid"] . $_POST["rank"] . '.txt';
$data[$line_num] == $replacewith;
$new_data = implode("\n",$data);//this will implode the array into one big text, fitted with line breaks.
$fp = fopen($cmdFileName,"w+");//this opens the file and clears it at the same time
fwrite($fp,$new_data);
fclose($fp);
}
}

 

Basically the "if" statment determines the correct line and this function sets the variable $line_num to the number of the line.

foreach ($cmdFilelookup as $line_num => $gamerfile) {

I already checked that and it works...

 

but its still not really doing anything to the file.

Err, let's try it this way.. Seeing how you're not seeing the beauty of a foreach o_o

 

$cmdFileName = "command.txt";
$data = file($cmdFileName);
$replacewith = 'modules/command/' . $_POST["memberid"] . $_POST["rank"] . '.txt';//this should be created first so we know what we're adding in
foreach ($data as $gamerfile) {
if (rtrim($gamerfile) == $_POST['gamerfile']) {//OMG! LOOK! A MATCH?!?!?! COOL!
$gamerfile = $replacewith;
}
}

$new_data = implode("\n",$data);//this will implode the array into one big text, fitted with line breaks.
$fp = fopen($cmdFileName,"w+");//this opens the file and clears it at the same time
fwrite($fp,$new_data);
fclose($fp);

 

Maybe you'll see what's happening here. You treated that foreach as if it was a "readonly" thing. In fact, as it loops through the array, you can literally pull that piece of the array out, make a change, and stick it back in. Almost like a laundromat where you have the shirts flying around on a conveyor belt... you take a shirt off and then put it back on the belt (if that makes sense).

 

So once we find the line to change, we'll change it then and there. Then, finish the foreach, implode the array, and done.

yeah I am rather new at this.. unfortunatly for my project i'm starting hard(er) and learning easy.

 

 

Ok so I believe its working cause I noticed its adding the extra "\n"

however I learned why I wasn't working before its just not working now like it should..

(using the code you provided)

HOWEVER, its still not changing the particular detail in the array... *sigh*

 

don't worry I know its not your fault, your trying your best trying to get me to understand what i'm trying to do and I appreciate it 290%!! I love to learn and doing my best, but i've have been working all day trying to get it to work.

Can you add a code in between some parts? we're going to see what's going on with the data...

 

After setting the variable, $replacewith, add the following line:

 

echo "Replace With: ".$replacewith;

 

This will tell us if we're getting the variable set right

 

Right before the $new_data = implode, add the following line:

 

print_r($data);

 

This will print out the array of the text file to see what is happening.

Here is the printout...

 

Replace With: modules/command/Otellinoc.txtArray ( [0] => modules/command/Patriotc.txt [1] => modules/command/Huntvc.txt [2] => modules/command/Vulcandeathm01s.txt [3] => modules/command/TopDogJWm01s.txt [4] => modules/command/EMHn01s.txt [5] => modules/command/Starwolfn01.txt [6] => modules/command/Dragonn02s.txt [7] => modules/command/Revengen02s.txt [8] => modules/command/Tycoonm02s.txt [9] => modules/command/Elitistm02.txt [10] => modules/command/DeepInCiderm04s.txt [11] => modules/command/Noradn04s.txt [12] => modules/command/MikeJonesm04.txt [13] => modules/command/Darknessm05.txt [14] => modules/command/Motherwolfn05.txt [15] => modules/command/LilYorkm06.txt [16] => modules/command/Metalmann06.txt [17] => modules/command/Shadown05.txt [18] => modules/command/xEminagzm05.txt [19] => modules/command/Otellinom08.txt [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => [28] => [29] => [30] => )

 

Yes its a compilation of file locations. lol. It would be hard to explain exactly why so don't ask.

 

but anywho yeah.

 

This was slightly stupid of me :-P $gamerfile is a variable created to represent the value of the array at that index... Changing it doesn't change the array xD

 

$cmdFileName = "command.txt";
$data = file($cmdFileName);
$replacewith = 'modules/command/' . $_POST["memberid"] . $_POST["rank"] . '.txt';//this should be created first so we know what we're adding in
foreach ($data as $index=>$gamerfile) {
if (rtrim($gamerfile) == $_POST['gamerfile']) {//OMG! LOOK! A MATCH?!?!?! COOL!
$data[$index] = $replacewith;
}
}

$new_data = implode("\n",$data);//this will implode the array into one big text, fitted with line breaks.
$fp = fopen($cmdFileName,"w+");//this opens the file and clears it at the same time
fwrite($fp,$new_data);
fclose($fp);

 

That code should work.

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.