簡體   English   中英

在PHP文件的兩行之間替換內容

[英]Replace content between two lines in a PHP file

我有一個PHP文件,目前正在編寫一個腳本,該腳本需要用另一個函數替換一個函數。 我想到的最好的方法是在兩個行號(將始終相同)之間查找126和136,然后在這兩行之間進行替換。

我不能做的是在修飾符腳本中包含任何原始功能,因此我不能只瀏覽文件中的功能名稱,等等(我正在創建一個腳本來修改另一個我沒有版權的付費腳本,所以我不能包含任何內容)。

我需要替換的功能是

function upload_supports_preview($upload) {

  if($upload->thumbnail != 'none') {

    return true;

  }

  return false;

}

我必須用

function upload_supports_preview($upload) {

  if($upload->type == 'video' || $upload->type == 'audio' || $upload->type == 'image') {

    return true;

  }

  return false;

}

對我來說,這聽起來不是一個好主意,這不是一個很好的解決方案,但它應該可以工作。

$filename = '/home/xyz.php';

// this loads the file into an array
$lines = file($filename);

// edit each line of code
$lines[126] = '// put some code here';
$lines[127] = '// put some code here';
$lines[128] = '// put some code here';
$lines[129] = '// put some code here';
$lines[130] = '// put some code here';
$lines[131] = '// put some code here';
$lines[132] = '// put some code here';
$lines[133] = '// put some code here';
$lines[134] = '// put some code here';
$lines[135] = '// put some code here';
$lines[136] = '// put some code here';

// write the file back to disk
// this joins the array back into a string, using the current end of line marker from your system, which could be different to what the source file used, but will still work
file_put_contents($filename, join(PHP_EOL, $lines));

runkit允許您在運行時runkit定義函數,然后可以按照需要的方式重新定義它。 這意味着您不必更改腳本,但可以注入所需的代碼。 但是,如果腳本是根據商業許可發布的,請檢查版權,很可能您侵犯了版權。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM