dual_alliance Posted August 10, 2006 Share Posted August 10, 2006 I know how to write to a file. But how do l actually write to a certain line of a file.Example:[b]settings.php[/b][code=php:0]// Game Details$gameName = "namehere"; // Line 3$gameSubName = "subnamehere";[/code]<br />[b]somefile.php[/b][code=php:0]$nGameName = $gameName;$nGameSubName = $gameSubName;$File = "settings.php";$Handle = fopen($File, 'w');$Data = "\$gameName = \"$nGameName\" ;\n ";fwrite($Handle, $Data);[/code]<br />How would l make it so it doesn't overwrite everything but only the information on line 3?Thanks,dual_alliance Quote Link to comment Share on other sites More sharing options...
litebearer Posted August 10, 2006 Share Posted August 10, 2006 Easiest way would be to read the entire file into an array, where each line is an element of the array. Then you can simply edit/alter/etc the specific element/line and write the array back to file. Quote Link to comment Share on other sites More sharing options...
dual_alliance Posted August 10, 2006 Author Share Posted August 10, 2006 Do you think you could please give me an example? Quote Link to comment Share on other sites More sharing options...
litebearer Posted August 10, 2006 Share Posted August 10, 2006 something like this[code]$handle = fopen($filename, "r");$contents = fread($handle, filesize($filename));fclose($handle);[/code]the variable $contents should be an array, where each element is a line from the file.if you wanted to replace line 4 in the file with "Its just a test"[code]$contents[3] = "Its just a test";[/code]then write the array back to the file Quote Link to comment Share on other sites More sharing options...
dual_alliance Posted August 10, 2006 Author Share Posted August 10, 2006 Thanks for your help litebearer ;D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.