更改 .ini 文件中键值的最佳方法是什么?

发布于 2024-11-17 22:24:04 字数 1551 浏览 4 评论 0原文

我有一项任务要完成,我已经尝试使用正则表达式几个小时但无济于事。一点帮助和一些教导将不胜感激。 .ini 文件格式是:

ES=Spanish
EN=English
TOKEN=Token
FILES=Files

我的例程的目标是

  1. 获取需要更改的键,
  2. 找出该键的当前值是什么,
  3. 添加一些有关旧值以及谁更改了它的注释,
  4. 然后更改该值。

因此,举例来说,如果我想将 EN 的值从 English 更改为 Eigo,我最终会得到:

ES=Spanish
#Changed by Jane Doe on <date>
#Old value: EN=English
EN=Eigo
TOKEN=Token
FILES=Files

我的代码:

        $content = "EN=English\n"
            . "ES=Spanish\n"
            . "TOKEN=Token\n"
            . "FILES=Files\n";

            $key = 'EN';
            $newValue = 'Eigo";
            $editor = 'Jane Doe';

             //get the old value
    $matches = array();
    preg_match('~'.$key.'\s?=[^\n$]+~iu',$content,$matches);

    $commentLines = '# Change by ' . $editor . ' on ' . date("m/d/Y g:i a") . "\n";
    $commentLines .= '# Old value: ' . $matches[0] . "\n";
    $newValueLine = $key.'='.$newValue. "\n";
    $newEntry = $commentLines . $newValueLine;
    $content = preg_replace('~'.$key.'\s?=[^\n$]+~iu',$newEntry,$content);

这工作正常,直到我意识到如果有人更改了短字符串的密钥,例如 EN ,然后我的正则表达式与 TOKEN 中的 EN 匹配,并且还更改了它,但弄乱了整个文件:

TOK#Changed by ....

所以,有几个问题:

  1. 因为每个 KEY 应该是唯一的,我是否应该为此使用正则表达式?我应该使用更快/更干净/更好的方法吗?

  2. 为什么当我将 ^ 添加到带有 preg_replace 的行上的正则表达式时,它不会与行的开头匹配并摆脱我的 TOKEN 匹配问题?

    preg_replace('~^'.$key.'\s?=[^\n$]+~iu',$newEntry,$content)
    

I have a task to accomplish, and I've been trying with regex for hours to no avail. A little help and some teaching would be appreciated. The .ini file format is:

ES=Spanish
EN=English
TOKEN=Token
FILES=Files

The goal of my routine is to

  1. Get a key that needs to be changed,
  2. Figure out what the current value of that key is,
  3. add some comments about the old value and who changed it,
  4. change the value.

So, for example, if I want to change the value belonging to EN from English to Eigo, I would end up with:

ES=Spanish
#Changed by Jane Doe on <date>
#Old value: EN=English
EN=Eigo
TOKEN=Token
FILES=Files

My code:

        $content = "EN=English\n"
            . "ES=Spanish\n"
            . "TOKEN=Token\n"
            . "FILES=Files\n";

            $key = 'EN';
            $newValue = 'Eigo";
            $editor = 'Jane Doe';

             //get the old value
    $matches = array();
    preg_match('~'.$key.'\s?=[^\n$]+~iu',$content,$matches);

    $commentLines = '# Change by ' . $editor . ' on ' . date("m/d/Y g:i a") . "\n";
    $commentLines .= '# Old value: ' . $matches[0] . "\n";
    $newValueLine = $key.'='.$newValue. "\n";
    $newEntry = $commentLines . $newValueLine;
    $content = preg_replace('~'.$key.'\s?=[^\n$]+~iu',$newEntry,$content);

This was working fine, until I realized that if someone changed the key for a short string, like EN, then my regex matches the EN in TOKEN, and also changes that, but messes up the whole file:

TOK#Changed by ....

So, a few questions:

  1. Because each KEY should be unique, should I even be using regex for this? Is there a faster/cleaner/better method I should be using?

  2. Why when I add a ^ to the regex on the line with preg_replace doesn't this match the beginning of the line and get rid of my TOKEN matching problem?

    preg_replace('~^'.$key.'\s?=[^\n$]+~iu',$newEntry,$content)
    

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

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

发布评论

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

评论(1

司马昭之心 2024-11-24 22:24:04

您提供的信息有点模糊。这是存储在文件中吗?如果是这样,那么您似乎真正想要的是像 Subversion 这样的版本控制软件。它将跟踪代码/ini 文件在任何时间点的状态、谁进行了更改,如果人们想要输入有关他们正在做什么的消息,它也会处理该问题。

你的正则表达式似乎有点过于复杂。对于多行搜索和替换,您需要使用 m 修饰符。

尝试:

$key = 'EN';
preg_replace('~^'.$key.'=.*$~m', $newEntry, $content);

http://www.spaweditor.com/scripts/regex/index.php 进行测试 与:

Regex: 
    /^EN=.*$/m

Data:
    EN=English
    ES=Spanish
    TOKEN=Token
    FILES=Files

Replace: 
    FOO=bar

Function:
    preg_replace

Result:
    FOO=bar
    ES=Spanish
    TOKEN=Token
    FILES=Files

The information you give is a bit vague. Is this being stored in a file? If so, it seems like what you really want is version control software like subversion. It will keep track of what the state of the code/ini files were like at any point in time, who made a change, and if people want to put in a message about what they were doing, it will handle that as well.

Your regex seems a bit overcomplicated. And for multiline search and replace, you need to use the m modifier.

Try:

$key = 'EN';
preg_replace('~^'.$key.'=.*$~m', $newEntry, $content);

Tested at http://www.spaweditor.com/scripts/regex/index.php with:

Regex: 
    /^EN=.*$/m

Data:
    EN=English
    ES=Spanish
    TOKEN=Token
    FILES=Files

Replace: 
    FOO=bar

Function:
    preg_replace

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