duankeye2342 2014-11-14 15:47
浏览 34
已采纳

可以在parse_ini_file配置文件中使用数学吗?

Can math be used in a parse_ini_file configuration file? If not, is there a recommended workaround?

$ini = parse_ini_file("config.ini");

config.ini

MAX_FILE_SIZE  =   100*pow(2,20)   ;100 MB
  • 写回答

3条回答 默认 最新

  • doucai7294 2014-11-14 16:00
    关注

    I've found a workaround for this!

    $a = parse_ini_file('a.ini', false, INI_SCANNER_RAW); 
    

    From PHP doc:

    If INI_SCANNER_RAW is supplied, then option values will not be parsed.
    

    So, you can collect the whole array, and then create a foreach loop, and use eval on the values.

    Check my code: (knowing bug that not remove the eval_next_line, but now i got to go. Monday i will fix that. Till that, you can fix also if you have time for it. Anyway, the round is happens. Yes, the error is that, i have the eval_next_line key twice, so i need to fix that. Anyway, monday.)

    define('EVAL_NEXT_LINE', 'eval_next_line');
    
    //Default removing the eval_next_lines
    $removeEvals = true;
    $options = parse_ini_file('a.ini', false, INI_SCANNER_RAW);
    if (array_key_exists("remove_evals", $options)) {
        if (empty($options['remove_evals']) || strtolower($options['remove_evals']) == 'no') {
            $removeEvals = false;
        }
    }
    
    $i = 0;
    $evalNextLine = false;
    foreach ($options as $key => $value) {
        if ($evalNextLine === true) {
            $options[$key] = eval("return " . $value .";");
            $evalNextLine = false;
        }
        if ($key == EVAL_NEXT_LINE && ($value || strtolower($options[EVAL_NEXT_LINE] = 'yes'))) {
            $evalNextLine = true;
            if ($removeEvals) {
                unset($options[$i]);
                //$i--;
            }
        }
        $i++;
    }
    
    var_dump($options);
    

    My a.ini is:

    [Parse options]
    remove_evals = 1;
    
    
    [Somethings]
    eval_next_line = true;
    myvar = round(2.5);
    
    abc = xyz;
    
    eval_next_line = true;
    myvar2 = 100*pow(2,20);
    
    [Somthing others]
    anythig = true;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭