Flat file: Purple Green Red Orange Pink Maroon Code (markup): I want to replace a line of my choice (dictated by a variable called $number) with the word "Blue." Thanks, Peter
Well, I don't think it's that simple. Instead of telling the script to replace "Orange," I want it to replace the line number of my choice (represented by $number).
There may be a better way, especially if you're handling larger files, but you could load the entire file into an array using file(), update the array (where line number-1 = array key) and save. $fileName = 'stuff.txt'; $lineNumber = 10; $changeTo = "the changed line\n\r"; // Load file into array $contents = file($fileName); // Replace line $contents[$lineNumber-1] = $changeTo; // Implode and save file_put_contents($fileName, implode('',$contents)); PHP:
Fatal error: Call to undefined function: file_put_contents() in /home/user/public_html/folder/folder/file.php on line 21
http://forums.digitalpoint.com/showthread.php?t=321249 The second post in that thread might be of some help. I just need to change it to replace the line, rather than delete it.
You have an old version of PHP. See this page for alternatives for file_put_contents(). http://www.php.net/file_put_contents