dsgwii4867 2014-09-04 10:28
浏览 407
已采纳

从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"];
  • 写回答

1条回答 默认 最新

  • dongzhuifeng1843 2014-09-04 10:40
    关注

    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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析