在INI文件中读取行注释

parse_ini_file
函数在读取配置文件时删除注释。 你会怎么做才能保留与下一行相关的评论? 例如:
[email]
; Verify that the email's domain has a mail exchange (MX) record.
validate_domain = 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>
还有其他想法吗?     
已邀请:
您可以使用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应该很容易。     

要回复问题请先登录注册