开发者

PHP - Mirror image of what parse_ini_file does?

开发者 https://www.devze.com 2023-01-06 22:42 出处:网络
parse_ini_file could be used to get configuration values from an ini file to use in the code, suppose your app changes some of those values and you want to set new defaults, is there a function to tak

parse_ini_file could be used to get configuration values from an ini file to use in the code, suppose your app changes some of those values and you want to set new defaults, is there a function to take the associative array and write the ini file o开发者_StackOverflowver?


You could use Zend Framework's Zend_Config_Writer_Ini to write the Ini File back to file. Because Zend Framework is component library, you can use only this component without having to migrate your entire application to ZF.

If you don't mind the config not being an Ini but PHP arrays, you can also use this code to load and write back the configs:

function config_read($filename)
{
    $config = include $filename;
    return $config;
}
function config_write($filename, array $config)
{
    $config = var_export($config, true);
    file_put_contents($filename, "<?php return $config ;");
}

Somewhat related question and my answer to it:

  • Reading and Writing Configuration Files


I don't remember having ever seen such function bundled with PHP...


But, to avoid re-inventing the wheel, maybe you could take a look at those two components of Zend Framework :

  • Zend_Config_Ini
  • Zend_Config_Writer

With a bit of luck, those might be easy to extract from the framework and use in your application... it's the case for some components, so maybe it is for those two...

0

精彩评论

暂无评论...
验证码 换一张
取 消