tobeyt23 Posted September 29, 2010 Share Posted September 29, 2010 I need to open a template file change a text var and then save the file as something new. How can I do this? Link to comment https://forums.phpfreaks.com/topic/214730-editing-a-file/ Share on other sites More sharing options...
BlueSkyIS Posted September 29, 2010 Share Posted September 29, 2010 fopen() and fread() the file, or use file() if the lines are distinct. find the text var to change, then save the updated content in a new file with a new fopen()/fwrite(). Link to comment https://forums.phpfreaks.com/topic/214730-editing-a-file/#findComment-1117211 Share on other sites More sharing options...
kenrbnsn Posted September 29, 2010 Share Posted September 29, 2010 You can also use file_get_contents and file_put_contents for reading & writing the file. Ken Link to comment https://forums.phpfreaks.com/topic/214730-editing-a-file/#findComment-1117215 Share on other sites More sharing options...
tobeyt23 Posted September 29, 2010 Author Share Posted September 29, 2010 This is what a came up with: <?php $_file = 'text.txt'; $_contents = file_get_contents($_file); $_replace = "Replaced"; $_find = '###FIND###'; $_contents = str_replace($_find, $_replace, $_contents); $_fo = fopen($_file,"w") or die ("Error editing."); fputs($_fo,$_contents); fclose($_fo) or die ("Error Closing File!"); ?> Link to comment https://forums.phpfreaks.com/topic/214730-editing-a-file/#findComment-1117222 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.