Jump to content

[SOLVED] fwrite() replace or delete line


grim1208

Recommended Posts

I have been trying a few different trial and error things on trying to figure out how I can read in a line, delete it, then replace it ...

 

this code, I tried with just overwriting whatever it is on that line, but only overwrites up to the length of the string it is I am wanting to write, and leaves the rest of the line the way it was currently

 

for example if it was $font = "something"; and I want to write $font = "poop"; the file would write to the line and look like $font = "poop";ing";

 

$file =  'styles.php';
$size = `wc -l $file`;
$size = preg_replace('/ .*$/', '', $size);

$handle = fopen($file,'r+');

$line_num = 2;

$line = '';
for($i=2; $i<=$line_num; $i++)
{
  $line = fgets($handle);
  fwrite($handle, '$tester');
  $f_color = explode('"', $line);
}
fclose($handle);

 

I know there has something to do with the number of bits I am writing, does anyone have any advice to this?

Link to comment
https://forums.phpfreaks.com/topic/137398-solved-fwrite-replace-or-delete-line/
Share on other sites

Thanks for the help guys, I took a little bit of both of your advice and came up w/ this

$fp = fopen("styles.php", "r");
$buffer = fread($fp, 120000);
fclose($fp);

$lines = explode("\n", $buffer);
$line = $lines[0];
$lines[1] = "\$font_color = '#FF0000';";

$fp = fopen("styles.php", "w");
$buffer = implode("\n", $lines)."\n";
fwrite($fp, $buffer);
fclose($fp);

Seems to work well, thanks again

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.