将文本添加到其上的另一个文件,而不会覆盖 php 中的任何内容


Add text to another file on top of it without overwriting anything in php

我想为我网站的所有文件添加 3 到 4 行,我可以修改所有 php 文件

 $source = file_get_contents($path);
 $source = str_replace($this->oldTxt, '', $source); 
 $source = preg_replace('#'<'?php#',"<?php'n".$this->newTxt,$source,1);
 file_put_contents($path, $source);
 echo $path."'n";

通过这段代码,我可以修改 php 文件,因为它们以 ?php 开头,但是我能做什么来将相同的文本添加到 js 和它顶部的其他文本文件中。

如果要将文本添加到顶部的所有文件中

$source = file_get_contents($path);
file_put_contents($path, $this->newTxt . $source);

任何特殊原因,您在<?php之后插入文本?注释语法在PHP,Javascript和HTML中是不同的。因此,您可能需要根据文件类型调整包含的文本。