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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值