grim1208 Posted December 17, 2008 Share Posted December 17, 2008 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 More sharing options...
Mark Baker Posted December 17, 2008 Share Posted December 17, 2008 Read the entire file into an array using file() $fileLines = file('myFile.php'); Replace the line you want: $fileLines[$lineNumber] = '$font = "poop";'; Write the entire file back Link to comment https://forums.phpfreaks.com/topic/137398-solved-fwrite-replace-or-delete-line/#findComment-717953 Share on other sites More sharing options...
.josh Posted December 17, 2008 Share Posted December 17, 2008 If you happen to know the maximum length any line can be, you can just pad it with $max-strlen(data) spaces Link to comment https://forums.phpfreaks.com/topic/137398-solved-fwrite-replace-or-delete-line/#findComment-717957 Share on other sites More sharing options...
grim1208 Posted December 17, 2008 Author Share Posted December 17, 2008 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 https://forums.phpfreaks.com/topic/137398-solved-fwrite-replace-or-delete-line/#findComment-718130 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.