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
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
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;