是否可以使用 PHP 对 .ini 文件使用内联注释?

发布于 2024-08-05 00:18:43 字数 99 浏览 1 评论 0原文

通过 PHP 对 .ini 文件使用内联注释是否可能且安全?

我更喜欢这样的系统:注释与变量内联,位于变量之后。

是否存在一些与要使用的语法有关的问题?

Is it possible and safe to use inline comments for .ini files with PHP?

I prefer a system where the comments are inline with the variables, coming after them.

Are the some gotchas concerning the syntax to be used?

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

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

发布评论

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

评论(3

汐鸠 2024-08-12 00:18:43

INI 格式 使用分号作为注释字符。它接受文件中的任何位置。

key1=value
; this is a comment
key2=value ; this is a comment too

INI format uses semicolon as a comment character. It accepts them anywhere in the file.

key1=value
; this is a comment
key2=value ; this is a comment too
陪我终i 2024-08-12 00:18:43

如果您正在谈论内置的 INI 文件解析功能,分号是它期望的注释字符,我相信它内联接受它们。

If you're talking about the built-in INI file parsing function, semicolon is the comment character it expects, and I believe it accepts them inline.

爱要勇敢去追 2024-08-12 00:18:43
<?php
$ini = <<<INI
; this is comment
[section]
x = y
z = "1"
foo = "bar" ; comment here!
quux = xyzzy ; comment here also!
a = b # not a comment
INI;

$inifile = tempnam(dirname(__FILE__), 'ini-temp__');
file_put_contents($inifile, $ini);
$a = parse_ini_file($inifile, true);
if ($a !== false)
{
  print_r($a);
}
else
{
  echo "Couldn't read '$inifile'";
}

unlink($inifile);

输出:

Array
(
    [section] => Array
        (
            [x] => y
            [z] => 1
            [foo] => bar
            [quux] => xyzzy
            [a] => b # not a comment
        )

)

请注意,PHP 的 INI 解析器不支持 # 注释。

<?php
$ini = <<<INI
; this is comment
[section]
x = y
z = "1"
foo = "bar" ; comment here!
quux = xyzzy ; comment here also!
a = b # not a comment
INI;

$inifile = tempnam(dirname(__FILE__), 'ini-temp__');
file_put_contents($inifile, $ini);
$a = parse_ini_file($inifile, true);
if ($a !== false)
{
  print_r($a);
}
else
{
  echo "Couldn't read '$inifile'";
}

unlink($inifile);

Outputs:

Array
(
    [section] => Array
        (
            [x] => y
            [z] => 1
            [foo] => bar
            [quux] => xyzzy
            [a] => b # not a comment
        )

)

Note that PHP's INI parser does not support # for comments.

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