读取INI文件中的行注释

发布于 2024-08-29 17:06:42 字数 575 浏览 2 评论 0原文

parse_ini_file 函数删除注释读取配置文件时。

您将如何保留与下一行相关的注释?

例如:

<前><代码>[电子邮件] ;验证电子邮件的域是否有邮件交换 (MX) 记录。 验证域 = true

我正在考虑使用 X(HT)ML 和 XSLT 将内容转换为 INI 文件(以便文档和选项可以是单一来源)。例如:

<h1>email</h1>
<p>Verify that the email's domain has a mail exchange (MX) record.</p>
<dl>
<dt>validate_domain</dt>
<dd>true</dd>
</dl>

还有其他想法吗?

The parse_ini_file function removes comments when reading configuration files.

What would you do to keep the comments that are associated with the next line?

For example:

[email]
; Verify that the email's domain has a mail exchange (MX) record.
validate_domain = true

Am thinking of using X(HT)ML and XSLT to transform the content into an INI file (so that the documentation and options can be single sourced). For example:

<h1>email</h1>
<p>Verify that the email's domain has a mail exchange (MX) record.</p>
<dl>
<dt>validate_domain</dt>
<dd>true</dd>
</dl>

Any other ideas?

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

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

发布评论

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

评论(1

娇俏 2024-09-05 17:06:42

您可以使用 preg_match_all 在 [heading] 标记之后提取注释:

$txt = file_get_contents("foo.ini");
preg_match_all('/\[([^\]]*)\][[:space:]]*;(.*)/',
    $txt, $matches, PREG_SET_ORDER);

$html = '';

foreach ($matches as $val) {
    $key = trim($val[1]); /* trimming to handle edge case
                             "[ email ]" so $key can be looked up
                              in the parsed .ini */
    $comment = $val[2];

    $html .= "<h1>$key</h1>\n";
    $html .= "<p>$comment</p>\n";
}

echo $html;

foo.ini 可能包含:

[email]
; Verify that the email's domain has a mail exchange (MX) record.
validate_domain = true ; comment ignored

[s2] ; comment can go here too
foo_bar = true

[s3]
foo_bar = true ; comment also ignored

我没有使用 parse_ini_file 因为我不想重新启动到另一个使用 PHP 5.3 的操作系统,但是我认为生成 HTML 的其余部分应该很容易。

You could use preg_match_all to extract comments after [heading] markups:

$txt = file_get_contents("foo.ini");
preg_match_all('/\[([^\]]*)\][[:space:]]*;(.*)/',
    $txt, $matches, PREG_SET_ORDER);

$html = '';

foreach ($matches as $val) {
    $key = trim($val[1]); /* trimming to handle edge case
                             "[ email ]" so $key can be looked up
                              in the parsed .ini */
    $comment = $val[2];

    $html .= "<h1>$key</h1>\n";
    $html .= "<p>$comment</p>\n";
}

echo $html;

foo.ini could contain:

[email]
; Verify that the email's domain has a mail exchange (MX) record.
validate_domain = true ; comment ignored

[s2] ; comment can go here too
foo_bar = true

[s3]
foo_bar = true ; comment also ignored

I didn't play around with parse_ini_file because I don't feel like rebooting to another OS with PHP 5.3, but I think it should be easy to generate the rest of the HTML.

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