我正在寻找使用 PHP 打开一个文件(参见下面的示例),逐行搜索字符串$colour并替换"="with之后的所有内容$value。
file.txt 之前:
red=0
green=23
blue=999
yellow=44
如果我$value是"1"并且我的颜色是"blue",我的文件应该更改为:
red=0
green=23
blue=1
yellow=44
到目前为止,我的代码是:
function write($colour, $value) {
$file = 'path';
$file_contents = file_get_contents($file);
$file_contents = str_replace($colour, $value, $file_contents);
file_put_contents($file, $file_contents);
}
然而,这只是$colour用$value(不是“=”之后的所有内容)替换我的输出:
red=0
green=23
1=999
yellow=44
我该怎么做呢?谢谢!