Last active
December 11, 2015 00:09
-
-
Save Ttech/4514384 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function better_parse_ini_file($filename){ | |
$ini_array = array(); | |
if(file_exists($filename)){ | |
$file_contents = file_get_contents($filename); | |
} else { | |
return false; | |
} | |
// we will now process the file | |
// Lets try parse_ini_file | |
if($parse_ini = parse_ini_file($filename)){ | |
$ini_array = $parse_ini; | |
} else { | |
$section_array = ''; | |
$lines = explode("\n",$file_contents); | |
foreach($lines as $line){ | |
// check for section blocks | |
if(preg_match('/^\[(.*?)\]\s*/', $line, $matches)){ | |
$ini_array[$matches[1]] = array(); // set array | |
$section_array = $matches[1]; | |
// everything else, check for option=value | |
} else { | |
if(preg_match('/^(\w+)(=| = )(.*)\s*/', $line, $options)){ | |
// this may be a bit messy | |
$ini_array[$section_array][$options[1]] = $options[3]; | |
} | |
} | |
} | |
} | |
// make it all pretty | |
return $ini_array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment