Skip to content

Instantly share code, notes, and snippets.

@Ttech
Last active December 11, 2015 00:09
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 Ttech/4514384 to your computer and use it in GitHub Desktop.
Save Ttech/4514384 to your computer and use it in GitHub Desktop.
<?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