更改 .ini 文件中键值的最佳方法是什么?
我有一项任务要完成,我已经尝试使用正则表达式几个小时但无济于事。一点帮助和一些教导将不胜感激。 .ini 文件格式是:
ES=Spanish
EN=English
TOKEN=Token
FILES=Files
我的例程的目标是
- 获取需要更改的键,
- 找出该键的当前值是什么,
- 添加一些有关旧值以及谁更改了它的注释,
- 然后更改该值。
因此,举例来说,如果我想将 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 ....
所以,有几个问题:
因为每个 KEY 应该是唯一的,我是否应该为此使用正则表达式?我应该使用更快/更干净/更好的方法吗?
为什么当我将
^
添加到带有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
- Get a key that needs to be changed,
- Figure out what the current value of that key is,
- add some comments about the old value and who changed it,
- 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:
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?
Why when I add a
^
to the regex on the line withpreg_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提供的信息有点模糊。这是存储在文件中吗?如果是这样,那么您似乎真正想要的是像 Subversion 这样的版本控制软件。它将跟踪代码/ini 文件在任何时间点的状态、谁进行了更改,如果人们想要输入有关他们正在做什么的消息,它也会处理该问题。
你的正则表达式似乎有点过于复杂。对于多行搜索和替换,您需要使用 m 修饰符。
尝试:
在 http://www.spaweditor.com/scripts/regex/index.php 进行测试 与:
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:
Tested at http://www.spaweditor.com/scripts/regex/index.php with: