PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP功能编辑配置文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在寻找一个PHP函数,可用于编辑文本文件中的键/值对.

我想做什么(PHP):

changeValue(key, bar);

并包含在seTings.txt中:

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,请注明来意。
标签: