search

Home  >  Q&A  >  body text

How to use file_get_contents to open a php file, search for the specified string, and add other content under it in a new line?

I want to implement a function, because every time the system I use is updated, it will overwrite the piece of code I added myself.
I want to implement this function. If it covers my code, I will open that piece of code. Add it again.

This is this code. If it is covered, I will open this file php file,
framework/bootstrap.inc.php

And search for this part, ['development'] == 1);
The complete thing is like this
define('DEVELOPMENT', $_W['config'] ['setting']['development'] == 1);

If true is returned after search, then a new line is added and a piece of code is appended after it
Like this
After search.

`define('DEVELOPMENT', $_W['config']['setting']['development'] == 1);`
// 追加的代码
define('DEBUG', $_W['config']['setting']['debug'] == 1);
if(DEBUG){
    include ROOT.'/framework/debug.php';
}
淡淡烟草味淡淡烟草味2699 days ago963

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-28 09:25:06

    Why not find the problem that some code will be overwritten when updating the code, but solve the problem in this roundabout way?

    reply
    0
  • 欧阳克

    欧阳克2017-06-28 09:25:06

    Why not

    define('DEBUG', $_W['config']['setting']['debug'] == 1);
    if(DEBUG) {
        include ROOT.'/framework/debug.php';
    }

    Create a separate file for this code you added, and then load both the system update file and your file into it

    require 'xxxx.php'; // 系统更新会覆盖的文件
    require 'bbbb.php'; // 你自己添加的代码

    In this way, you don’t have to worry about it being overwritten by the system after updating, and it is much easier to maintain your own code.

    reply
    0
  • PHP中文网

    PHP中文网2017-06-28 09:25:06

    I agree with the point above. But if you have to do this, you can add a comment to the code you added. Then read the contents of the entire file and use comments to split the file into two strings. Then concatenate your code with the two strings, and finally write it to the file.

    reply
    0
  • Cancelreply