是否有任何特殊的函数来解析php中的.conf文件,如parse_ini_file()函数? 如果不是,我怎么能实现这一目标? 谢谢!
编辑:
Conf就像httpd.conf(Apache)。 (我想阅读和编辑httpd.conf文件)
Is there any special function to parse a .conf file in php like parse_ini_file() function? If not how can I achieve that? Thanks!
EDIT :
Conf's like httpd.conf(Apache). (I want to read and edit httpd.conf file)
最满意答案
不,解析httpd.conf没有特殊的功能,但解析器应该很容易编写。 例如,如果您只对键值设置感兴趣,比如ServerRoot /var/www ,那么这样就可以了:
<?php define('HTTPD_CONF', '/tmp/httpd.conf'); $lines = file(HTTPD_CONF); $config = array(); foreach ($lines as $l) { preg_match("/^(?P<key>\w+)\s+(?P<value>.*)/", $l, $matches); if (isset($matches['key'])) { $config[$matches['key']] = $matches['value']; } } var_dump($config);如果你想解析<Directory ...>和其他块,它会带你几行代码,但它不应该太费力。 这真的取决于你想做什么。 您可以从$_SERVER和getenv()获取大量信息,因此您可能不需要解析配置文件。
编辑
为了响应您的更新,要编辑httpd.conf,您需要以超级用户权限运行脚本并导致重新加载httpd.conf(例如, system("apachectl graceful"); )。
No, there is no special function to parse httpd.conf, but a parser should be easy to write. For example, if you're only interested in the key-value settings, like ServerRoot /var/www, then this will do the trick:
<?php define('HTTPD_CONF', '/tmp/httpd.conf'); $lines = file(HTTPD_CONF); $config = array(); foreach ($lines as $l) { preg_match("/^(?P<key>\w+)\s+(?P<value>.*)/", $l, $matches); if (isset($matches['key'])) { $config[$matches['key']] = $matches['value']; } } var_dump($config);If you want to parse the <Directory ...> and other blocks, it'll take you a few more lines of code, but it shouldn't be too taxing. It really depends on what you want to do. You can get a lot of info from $_SERVER and getenv(), so you might not need to parse a config file.
EDIT
In response to your update, for editing httpd.conf, you'll need to run your script with superuser privileges and cause httpd.conf to be reloaded (e.g., system("apachectl graceful");).
更多推荐
发布评论