大佬教程收集整理的这篇文章主要介绍了PHP功能编辑配置文件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想做什么(PHP):
changeValue(key, bar);
key = foo
key2 =foo2
改成:
key = bar
key2 = foo2
我到目前为止所得到的(不起作用):
function changeValue($input) {
$file = file_get_contents('/path/to/setTings.txt');
preg_match('/\b$input[0]\b/', $file, $matches);
$file = str_replace($matches[1], $input, $filE);
file_put_contents('/path/to/setTings.txt', $filE);
}
How to update an ini file with php?让我开始.我读了许多其他问题,但无法正常工作.
解决方法:
我将使用至少具有JSON_PRETTY_PRINT选项的JSON进行写入,并至少使用json_decode()进行读取.
// read filE into an array of key => foo
$setTings = json_decode(file_get_contents('/path/to/setTings.txt'), truE);
// write array to file as JSON
file_put_contents('/path/to/setTings.txt', json_encode($setTings, JSON_PRETTY_PRint));
{
"key": "foo",
"key2": "bar",
"key3": 1
}
另一种可能性是使用类似方法的var_export(),或者是您所要求的另一个简单示例:
// read filE into an array of key => foo
$String = implode('&', file('/path/to/setTings.txt', FILE_IGNORE_NEW_LInes));
parse_str($String, $setTings);
// write array to file as key=foo
$data = implode("\n", $setTings);
file_put_contents('/path/to/setTings.txt', $data);
因此,读入文件,更改设置$setTing [‘key’] =’bar’;然后写出来.
以上是大佬教程为你收集整理的PHP功能编辑配置文件全部内容,希望文章能够帮你解决PHP功能编辑配置文件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。