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

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

 

?>

 

 

 

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>

 

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>

 

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>

 

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.