Skip to content

Instantly share code, notes, and snippets.

@dordenis
Created October 17, 2014 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dordenis/1e0924c076291628ec61 to your computer and use it in GitHub Desktop.
Save dordenis/1e0924c076291628ec61 to your computer and use it in GitHub Desktop.
<?php
$handle = @fopen("test.ini", "r");
if (! $handle) {
fclose($handle);
return;
}
$res = array();
while (($line = fgets($handle, 4096)) !== false) {
preg_match("/\[(\w+)\]/i", $line, $key);
if (isset($key[1])) {
$res[$key[1]] = array();
$current = &$res[$key[1]];
} else {
preg_match("/([\w.]+)\s+=\s+([^\s]+)/i", $line, $match);
if (isset($match[1])) {
$keys = preg_split('/\./', $match[1]);
setKey($keys, $match[2], $current);
}
}
}
function setKey($keys, $value, &$a) {
$key = array_shift($keys);
if(is_null($key)) {
$a = $value;
return;
}
if (! isset($a[$key])) {
$a[$key] = array();
}
if (! is_array($a[$key])) {
$val = $a[$key];
$a['_value'] = $val;
$a[$key] = array();
}
return setKey($keys, $value, $a[$key]);
}
print_r($res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment