PHP fwrite &附加到 PHP 函数

发布于 2025-01-08 01:35:28 字数 461 浏览 0 评论 0原文

我正在使用 fopen 访问我的 PHP 文件:

$readFd = @fopen($file, 'r+');

我想在该文件中搜索函数调用 parent::process(); 如果存在,我会在此之后插入一个新的函数调用。

我尝试过使用 preg_replace 但它似乎与parent::process(); 不匹配 比如我需要的结果是这样的。

public function process() {
  parent::process();
  $this->newFunction();
}

然后将其写入我正在使用的文件:

fwrite($readFd, $content);

我想我一定遗漏了正则表达式的一些重要内容。 希望有人能指出我正确的方向。

I am using fopen to reach my PHP file :

$readFd = @fopen($file, 'r+');

I would like to search this file for the function call parent::process();
And if this exists I would then insert a new function call after this.

I have tried using preg_replace but it does not seem to match parent::process();
For example the result I need is this.

public function process() {
  parent::process();
  $this->newFunction();
}

Then to write the to the file I am using :

fwrite($readFd, $content);

I guess I must be missing something important with regex.
Hopefully someone can point me in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

待天淡蓝洁白时 2025-01-15 01:35:28

我将使用 php 函数 fgets 读取文件中的每个增加 1 直到到达您需要的行。然后您的指针将位于该行之后,您可以在其中编写自己的行。

编辑

我错了,当您在特定点向文件写入内容时,该点之后的所有内容都会丢失。所以我做了一些测试并提出了这个:

$handle = fopen($file,"r+");

$lines = array();
while(($line = fgets($handle)) !== false) {
    $lines[] = $line;
    if(strpos($line, 'parent::process()')) {
        $lines[] = '$this->newFunction();';
    }
 }
 fseek($handle, 0); // reset pointer
 foreach($lines as $line) {
    fwrite($handle, $line);
 }
 fclose($handle);

我希望这可以解决您的问题。

I would use the php function fgets to read every in the file one by one until you reach the line you need. And then your pointer will be after that line where you can write your own line.

EDIT

I was wrong, when you write something to a file at a specific point, everything after that point is lost. So I did a little testing and came up with this:

$handle = fopen($file,"r+");

$lines = array();
while(($line = fgets($handle)) !== false) {
    $lines[] = $line;
    if(strpos($line, 'parent::process()')) {
        $lines[] = '$this->newFunction();';
    }
 }
 fseek($handle, 0); // reset pointer
 foreach($lines as $line) {
    fwrite($handle, $line);
 }
 fclose($handle);

I hope this solves your problem.

冰雪之触 2025-01-15 01:35:28

我想出了解决方案,但是您的代码似乎更短,所以我明天会尝试您的解决方案。

if(! $readFd = @fopen($file, "r+"))
  return FALSE;

$buffer = fread($readFd, 120000);
fclose($readFd);
$onDuplicate = FALSE;
$lines = explode("\n", $buffer);
foreach($lines AS $key => $line) {
  if(strpos($line, "newFunction()")) {
    $onDuplicate = TRUE;
  }
  if(strpos($line, "parent::process()")) {
    $lines[$key] = "\t\tparent::process();\n\t\t//\$this->newFunction();\n";
  }
}

if(! $onDuplicate) {
  $readFd = fopen($file, "w");
  $buffer = implode("\n", $lines)."\n";
  fwrite($readFd, $buffer);
  fclose($readFd);
} else {
  var_dump('changes are already applied');
}

感谢您的帮助!

I came up with the solution however your code seems much shorter so I will try your solution tomorrow.

if(! $readFd = @fopen($file, "r+"))
  return FALSE;

$buffer = fread($readFd, 120000);
fclose($readFd);
$onDuplicate = FALSE;
$lines = explode("\n", $buffer);
foreach($lines AS $key => $line) {
  if(strpos($line, "newFunction()")) {
    $onDuplicate = TRUE;
  }
  if(strpos($line, "parent::process()")) {
    $lines[$key] = "\t\tparent::process();\n\t\t//\$this->newFunction();\n";
  }
}

if(! $onDuplicate) {
  $readFd = fopen($file, "w");
  $buffer = implode("\n", $lines)."\n";
  fwrite($readFd, $buffer);
  fclose($readFd);
} else {
  var_dump('changes are already applied');
}

Thanks for all your help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文