从ini文件中读取分号分隔的字符串值

从ini文件中读取分号分隔的字符串值

问题描述:

I am reading configuration file, I want to read a name which contains ; in string like

config=node1;node2;node3

i want to read as a whole instead of node1 as replied by php in reading

my code:

$ini_array = parse_ini_file("test_config.conf", true);
$nodelist = $ini_array["config"];

我正在阅读配置文件,我想在字符串中读取包含; code>的名称 喜欢 p>

  config = node1; node2; node3 
  code>  pre> 
 
 

我想整体阅读而不是

我的代码中的> node1 code>: p>

  $ ini_array = parse_ini_file(“test_config.conf”  ,true); 
 $ nodelist = $ ini_array [“config”]; 
  code>  pre> 
  div>

Since a ; indicates a comment in an ini file, parse_ini_file() will correctly throw away everything behind a (non quoted) ;.

For me it looks like you are misusing the ini format, thus if you want to obtain that information, you need to parse the file on your own, like this:

$config = array();
foreach(file('/path/to/conf.ini') as $line) {
    if(preg_match('/^([^;]*?)=(.*)/', $line, $m)) {
        $config[$m[1]] = $m[2];
    }
}

var_dump($config);

If you only need the value of config, you can use this:

preg_match('/^\s*config\s*=(.*)/', file_get_contents('config.ini'), $m);
echo $config = $m[1];