Created
May 11, 2012 19:33
-
-
Save mlconnor/2661946 to your computer and use it in GitHub Desktop.
My Parse Ini Implementation in PHP
This file contains hidden or 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
function myParseIni($iniStr, $processSections = false) { | |
// this will split on lines and then remove comments | |
$lines = preg_grep('|^\s*;|', preg_split('/$\R?^/m', $iniStr), PREG_GREP_INVERT); | |
$results = array(); | |
$section = null; | |
foreach ($lines as $line) { | |
// print $line; | |
$matches = array(); | |
if ( $processSections && preg_match('/^\s*\[\s*([^\s:]+)\s*(?::\s*(\S+))?\s*\]\s*$/', $line, $matches) ) { | |
$sectionName = $matches[1]; | |
if ( count($matches) > 2 ) { | |
print "FOUND EXT " . print_r($matches[1], true) . "\n"; | |
if ( ! isset($results[$matches[2]]) ) { | |
throw new Exception("attempt to extend class '$matches[2]' but it wasn't defined " . print_r($results, true)); | |
} | |
// this is an extension | |
$section = $results[$matches[2]]; | |
$results[$sectionName] =& $section; | |
//print "EXTENDS $matches[2]\n"; | |
} else { | |
print "CREATING SECTION $sectionName \n"; | |
$results[$sectionName] = array(); | |
$section =& $results[$sectionName]; | |
} | |
} else if ( preg_match('/^\s*([^=\s]+)\s*=\s*(.*)$/', $line, $matches) ) { | |
//print "KEY=$matches[1] VAL=$matches[2] SECTION=[$section]\n"; | |
$chain = explode('.', $matches[1]); | |
$value = $matches[2]; | |
//if ( is_array($section) ) { | |
if ( $processSections ) { | |
if ( is_array($section) ) { | |
$current =& $section; | |
} else { | |
continue; | |
} | |
} else { | |
$current =& $results; | |
} | |
// let's make sure the value is of the correct type. | |
if ( preg_match('|^\s*null\s*$|i', $value) ) { | |
$value = null; | |
} else if ( preg_match('|^\s*true\s*$|i', $value) ) { | |
$value = TRUE; | |
} else if ( preg_match('|^\s*false\s*$|i', $value) ) { | |
$value = false; | |
} else if ( is_numeric($value) ) { | |
$value = 0 + $value; | |
} else if ( preg_match('|^\s*"(.*)"\s*$|', $value, $strMatch) ) { | |
$value = $strMatch[1]; | |
} | |
for ( $i = 0; $i < count($chain) - 1; $i++ ) { | |
$link = $chain[$i]; | |
//print "looking for prop $link in obj\n"; | |
if ( isset($current[$link]) ) { | |
$current =& $current[$link]; | |
} else { | |
$current[$link] = array(); | |
$current =& $current[$link]; | |
} | |
} | |
$finalPropName = $chain[count($chain) - 1]; | |
if ( preg_match('|\[\]$|', $finalPropName) ) { | |
$finalPropName = preg_replace('|\[\]|', '', $finalPropName); | |
if ( isset($current[$finalPropName]) ) { | |
$current[$current[$finalPropName]][] = $value; | |
} else { | |
$current[$finalPropName] = array($value); | |
} | |
} else { | |
$current[$finalPropName] = $value; | |
} | |
//print "setting $matches[1] to $matches[2] with " . print_r($current, true) . " with prop " . $finalProp . "\n"; | |
// ok, now we know where we are, let's set it. | |
// $current[$chain[count($chain) - 1]] = $matches[2]; // value | |
//} // we have a section | |
} | |
} | |
//print_r($results); | |
return $results; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment