Jump to content

Replace or Delete a line in an external text file


aleksandra

Recommended Posts

Hello

I need to change the last line of an external text file

The file consists of

 

Last Connection: 1/20/2008 1:44:08 PM

Indoor Temperature: 73

Outdoor Temperature: 21

Indoor Setpoint: 72

New Setpoint: 55

 

I need to know how to change the last line (replace it with a user input)

 

Thank you

Link to comment
Share on other sites

ok this is how i started it trying to separate lines and only change the last one and i'm very stuck

 

<?php

 

 

$file_handle = fopen("filename.txt", "rb");

 

while (!feof($file_handle) ) {

 

$line_of_text = fgets($file_handle);

$parts = explode('=', $line_of_text);

 

print $line_of_text. "<BR>";

}

 

 

fclose($file_handle);

 

?>

 

 

 

Link to comment
Share on other sites

Ok this does what i need it to do

I suck at php so i'm sure this code is messy as hell so if anyone can clean it up or make it work for an unknown number of lines in a text file that would be great

 

<?PHP

 

$text ="The new set point is: ". $_POST['shit'];

 

$myFile = "file.txt";

$lines = file($myFile);

$fh = fopen($myFile, 'w') or die("Can not open file");

foreach ($lines as $line_num => $line) {

 

}

$stringData =$lines[0].$lines[1].$lines[2].$lines[3].$text;

fwrite($fh, $stringData);

 

$lines = file('file.txt');

 

// Loop through our array

foreach ($lines as $line_num => $line) {

    echo ($line);?><br/>

<?php

}

 

?>

 

<form action="process1.php" method="post">

<textarea name="shit"></textarea>

<input type="submit" />

</form>

 

Link to comment
Share on other sites

eeewwww :P

HAHA

 

<?
$text ="The new set point is: ". $_POST['shit'];
$lines=file('file.txt');
array_pop ($lines);
array_push ($lines,"new text");
file_put_contents('file.txt',$lines);
echo implode("<br>",$file);
?>

<form action="process1.php" method="post">
<textarea name="shit"></textarea>
<input type="submit" />
</form>

 

Link to comment
Share on other sites

Edit

 

<?
$text ="The new set point is: ". $_POST['shit'];
$lines=file('file.txt');
array_pop ($lines);
array_push ($lines,"$text");
file_put_contents('file.txt',$lines);
echo implode("<br>",$lines);
?>

<form action="process1.php" method="post">
<input type=text name="shit" size=5>
<input type="submit">
</form>

 

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.