【发布时间】:2020-11-11 03:32:31
【问题描述】:
我希望使用 PHP 打开一个文件(参见下面的示例),逐行搜索字符串 $colour 并将 "=" 之后的所有内容替换为 $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
我该怎么做?谢谢!
【问题讨论】:
标签: php file file-handling write