替换PHP文件中两行之间的内容

时间:2013-08-12 06:17:26

标签: php

我有一个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;

}

2 个答案:

答案 0 :(得分:1)

这对我来说听起来不是一个好主意,这不是一个非常好的解决方案,但它应该有效。

$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));

答案 1 :(得分:0)

runkit允许您在运行时取消定义函数,然后您可以按照您希望的方式重新定义它。这意味着您不必更改脚本,但可以注入所需的代码。如果脚本是在商业许可下发布的,则检查版权,但很可能是您违反了版权。