davocold Posted November 13, 2009 Share Posted November 13, 2009 how do i replace a specific line in a text file with an array string? example: text content is like this: red,brown,green,black black,green,red,brown green,black,brown,red so if i have $array = "purple,grey,pink,cyan"; how can i use fwrite to replace line 2 in text file which is green,black,brown,red with $array i want to replace the whole line 2 with the arrray string. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 13, 2009 Share Posted November 13, 2009 Something like this (Untested): $line = 1; // Change to the line you want to replace (0 is the first line) $array = "purple,grey,pink,cyan"; $file = "myfile.txt"; $handle = fopen($file, "r"); $contents = fread($handle, filesize($file)); fclose($handle); $arrCont = explode("\n", $contents); $arrCont[$line] = $array; $handle = fopen($file, "w+"); fwrite($handle, implode("\n", $arrCont)); fclose($handle); 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.