hungryfrank Posted January 23, 2020 Share Posted January 23, 2020 hi I am trying to add a few line of code using fopen It works and it adds the lines but I have multiple occurrence of this line and I would like to all be amended. $target_line='$second = (int)substr($raw_date, 17, 2);'; $lines_to_add= '$raw_datetime = translate_from_gregorian($raw_datetime);'. PHP_EOL. '$year = (int)substr($raw_datetime, 0, 4);'. PHP_EOL. '$month = (int)substr($raw_datetime, 5, 2);'. PHP_EOL. '$day = (int)substr($raw_datetime, 8, 2);'. PHP_EOL; $config ='includes/functions/general.php'; $file=fopen($config,"r+") or exit("Unable to open file!"); $insertPos=0; // variable for saving //Users position while (!feof($file)) { $line=fgets($file); if (strpos($line,$target_line)!==false) { $insertPos=ftell($file); // ftell will tell the position where the pointer moved, here is the new line after //Users. $newline = $lines_to_add; } else { $newline.=$line; // append existing data with new data of user } } fseek($file,$insertPos); // move pointer to the file position where we saved above fwrite($file, $newline); fclose($file); Quote Link to comment https://forums.phpfreaks.com/topic/309921-how-to-open-and-add-after-multiple-occurence-of-a-line/ Share on other sites More sharing options...
ginerjm Posted January 23, 2020 Share Posted January 23, 2020 (edited) I think I know what you are trying to do and that is to modify a block of code that exists in several files. So - my suggestion is to stop and re-think your actions here. Why not alter all those lines to one call to a function passing in a variable? Then write a stand-alone module that contains this function and have it do the work. This way if you need to change this once again, you would only have to worry about one existence of this code. Edited January 23, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/309921-how-to-open-and-add-after-multiple-occurence-of-a-line/#findComment-1573746 Share on other sites More sharing options...
hungryfrank Posted January 23, 2020 Author Share Posted January 23, 2020 (edited) I am trying to. modify existing file in a php script to add a module for world calendars. I want to make these changes like this so if in the future there is an upgrade to the original script I can make these changes without remembering what happened. there is only two filets change and either 0,1,2 occurrences of this line. it is always the same and hasn't changed in 15 years Edited January 23, 2020 by hungryfrank Quote Link to comment https://forums.phpfreaks.com/topic/309921-how-to-open-and-add-after-multiple-occurence-of-a-line/#findComment-1573750 Share on other sites More sharing options...
ginerjm Posted January 23, 2020 Share Posted January 23, 2020 So - by now you should have managed to make these 2 changes manually. So - what's the problem? Quote Link to comment https://forums.phpfreaks.com/topic/309921-how-to-open-and-add-after-multiple-occurence-of-a-line/#findComment-1573751 Share on other sites More sharing options...
hungryfrank Posted January 23, 2020 Author Share Posted January 23, 2020 yes I can change them manually but lets say inn a few months there is an upgrade to the main script. this way I can install my calendar module and all the necessary changes are made. I don't have to remember what they were Quote Link to comment https://forums.phpfreaks.com/topic/309921-how-to-open-and-add-after-multiple-occurence-of-a-line/#findComment-1573753 Share on other sites More sharing options...
hungryfrank Posted January 23, 2020 Author Share Posted January 23, 2020 (edited) different people think differently. not everything has to fit in your box. and they don't have to have a cubic box either thanks Edited January 23, 2020 by hungryfrank Quote Link to comment https://forums.phpfreaks.com/topic/309921-how-to-open-and-add-after-multiple-occurence-of-a-line/#findComment-1573755 Share on other sites More sharing options...
Barand Posted January 23, 2020 Share Posted January 23, 2020 11 minutes ago, hungryfrank said: different people think differently Very true. The U.S. spent millions developing a ballpoint pen that would operate at any angle and in weightless conditions. The Russians gave their cosmonauts pencils instead. Quote Link to comment https://forums.phpfreaks.com/topic/309921-how-to-open-and-add-after-multiple-occurence-of-a-line/#findComment-1573756 Share on other sites More sharing options...
hungryfrank Posted January 23, 2020 Author Share Posted January 23, 2020 (edited) ad who is to say which one made a better choice Edited January 23, 2020 by hungryfrank Quote Link to comment https://forums.phpfreaks.com/topic/309921-how-to-open-and-add-after-multiple-occurence-of-a-line/#findComment-1573761 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.