Skip to content

Instantly share code, notes, and snippets.

@littlefuntik
Last active April 25, 2017 05:10
Show Gist options
  • Save littlefuntik/c6882e5bdca3b5b8633bbc04fbece8e6 to your computer and use it in GitHub Desktop.
Save littlefuntik/c6882e5bdca3b5b8633bbc04fbece8e6 to your computer and use it in GitHub Desktop.
<?php
/*
file "uk.ini" content:
[section1]
value1={{.REPLACE1}}
[section2]
value0={{.REPLACE2}}
test=test
[test{{.REPLACE2}}]
not=found
print_r($result) :
Array
(
[section1] => Array
(
[value1] => Vasia
)
[section2] => Array
(
[value0] => Pupkin
[test] => test
)
[testPupkin] => Array
(
[not] => found
)
)
*/
/**
* Parse a configuration file
* @param string $filename
* @param array $replaces [optional]
* @param bool $process_sections [optional]
* @param int $scanner_mode [optional]
* @return array|bool The settings are returned as an associative array on success, and false on failure.
* @since 4.0
* @since 5.0
*/
function parse_ini_file_replaces($file_name, array $replaces = [], $process_sections = false, $scanner_mode = INI_SCANNER_NORMAL) {
$content = file_get_contents($file_name);
if (false === $content) {
throw new \InvalidArgumentException('Bad file');
}
$content = str_ireplace(array_keys($replaces), array_values($replaces), $content);
return call_user_func('parse_ini_string', $content, $process_sections, $scanner_mode);
}
$result = parse_ini_file_replaces(__DIR__ . '/uk.ini', [
'{{.REPLACE1}}' => 'Vasia',
'{{.REPLACE2}}' => 'Pupkin',
], true);
print_r($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment