如何使用 php 更新 ini 文件?

发布于 2024-09-13 19:15:34 字数 351 浏览 1 评论 0原文

我有一个已创建的现有 ini 文件,我想知道是否有办法更新该文件的一部分,还是每次都必须重写整个文件?

以下是我的 config.ini 文件的示例:

[config]
    title='test'
    status=0
[positions]
    top=true
    sidebar=true
    content=true
    footer=false

假设我想更改 [positions] top=false。那么我是否会使用 parse_ini_file 来获取所有信息,然后进行更改并使用 fwrite 重写整个文件。或者有没有办法改变该部分?

I have an existing ini file that I have created and I would like to know if there was a way to update a section of the file or do I have have to rewrite the entire file each time?

Here is an example of my config.ini file:

[config]
    title='test'
    status=0
[positions]
    top=true
    sidebar=true
    content=true
    footer=false

Say I want to change the [positions] top=false. So would I use the parse_ini_file to get all of the infromation then make my changes and use fwrite to rewrite the whole file. Or is there a way just to change that section?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林迷了鹿 2024-09-20 19:15:34

我使用了你的第一个建议:

那么我会使用 parse_ini_file 来获取所有信息,然后进行更改并使用 fwrite 重写整个文件

function config_set($config_file, $section, $key, $value) {
    $config_data = parse_ini_file($config_file, true);
    $config_data[$section][$key] = $value;
    $new_content = '';
    foreach ($config_data as $section => $section_content) {
        $section_content = array_map(function($value, $key) {
            return "$key=$value";
        }, array_values($section_content), array_keys($section_content));
        $section_content = implode("\n", $section_content);
        $new_content .= "[$section]\n$section_content\n";
    }
    file_put_contents($config_file, $new_content);
}

I used the first suggestion of you:

So would I use the parse_ini_file to get all of the infromation then make my changes and use fwrite to rewrite the whole file

function config_set($config_file, $section, $key, $value) {
    $config_data = parse_ini_file($config_file, true);
    $config_data[$section][$key] = $value;
    $new_content = '';
    foreach ($config_data as $section => $section_content) {
        $section_content = array_map(function($value, $key) {
            return "$key=$value";
        }, array_values($section_content), array_keys($section_content));
        $section_content = implode("\n", $section_content);
        $new_content .= "[$section]\n$section_content\n";
    }
    file_put_contents($config_file, $new_content);
}
治碍 2024-09-20 19:15:34

如果使用 PHP INI 函数,则每次都必须重写该文件。

如果您编写自己的处理器,则可以(有限制)就地更新。如果插入的内容比删除的内容长或短,则无论如何您都必须重写文件。

If you use the PHP INI functions, you must rewrite the file each time.

If you write your own processor, you could (with limitations) update in place. If your insertions are longer or shorter than your deletions, you'll have to rewrite the file anyhow.

猫烠⑼条掵仅有一顆心 2024-09-20 19:15:34

这是一个完美的示例,说明您可以使用正则表达式来替换文本字符串。查看 preg_replace 函数。如果您不太确定如何使用正则表达式,您可以在此处找到一个很棒的

教程澄清你需要做这样的事情:

<?php

$contents = file_get_contents("your file name");
$contents = preg_replace($pattern, $replacement, $contents);

$fh = fopen("your file name", "w");
fwrite($fh, $contents);

?>

其中 $pattern 是你要匹配的正则表达式, $replacement 是你的替换值。

This is a perfect example of when you could use regular expressions to replace a string of text. Check out the preg_replace function. If you're not quite sure how to use regular expressions you can find a great tutorial here

Just to clarify you'll need to do something like this:

<?php

$contents = file_get_contents("your file name");
$contents = preg_replace($pattern, $replacement, $contents);

$fh = fopen("your file name", "w");
fwrite($fh, $contents);

?>

Where $pattern is your regex to match and $replacement is your replacement value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文