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>
还有其他想法吗?
答案 0 :(得分:1)
您可以使用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的其余部分应该很容易。