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

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.