创建ini文件,在PHP中写入值
我找不到一种方法可以轻松地让我创建一个新文件,将其视为一个 ini 文件(不是 php.ini 或类似的...每个用户的单独 ini 文件),并使用 PHP 创建/删除值。 PHP 似乎没有提供创建 ini 文件和读/写/删除值的简单方法。 到目前为止,这一切都只是“读取”——与创建条目或操作键/值无关。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从PHP文档的注释中找到以下代码片段:
用法:
祝你好运!
Found following code snippet from the comments of the PHP documentation:
Usage:
Good luck!
我使用这个,它似乎有效,
它返回要写入 .ini 文件的文本
I use this and it seems to work
it returns the text to write to an .ini file
基于上面的答案,我写了这个可能有用的课程。 适用于 PHP 5.3,但可以轻松适应以前的版本。
based on the above answers I wrote this class that might be useful. For PHP 5.3 but can be easily adapted for previous versions.
在这部分代码中:
$key2 必须替换为 $key,否则您会在 .ini 中找到空键
in this portion of code:
$key2 must be replaced by $key or you would find empty keys in your .ini
PEAR 有两个(经过单元测试的)包,它们可以完成您渴望的任务:
.ini
文件.php
和。 xml
文件我宁愿使用经过良好测试的代码,也不愿编写自己的代码。
PEAR has two (unit tested) packages which do the task you are longing for:
.ini
files.php
and.xml
filesI'd rather use well tested code than writing my own.
我无法保证它的工作效果如何,但有一些建议可以实现与 parse_ini_file< 的文档页面上的 ="nofollow noreferrer">
parse_ini_file()
(即write_ini_file
,这不是标准 PHP 函数) /代码>。您可以使用
write_ini_file
将值发送到文件,使用parse_ini_file
将它们读回 - 修改parse_ini_file
返回的关联数组,然后使用 write_ini_file 将修改后的数组写回文件。那对你有用吗?
I can't vouch for how well it works, but there's some suggestions for implementing the opposite of
parse_ini_file()
(i.e.write_ini_file
, which isn't a standard PHP function) on the documentation page forparse_ini_file
.You can use
write_ini_file
to send the values to a file,parse_ini_file
to read them back in - modify the associative array thatparse_ini_file
returns, and then write the modified array back to the file withwrite_ini_file
.Does that work for you?