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. Link to comment https://forums.phpfreaks.com/topic/181345-how-do-i-replace-a-specific-line-in-a-text-file-using-php/ 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); Link to comment https://forums.phpfreaks.com/topic/181345-how-do-i-replace-a-specific-line-in-a-text-file-using-php/#findComment-956650 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.