Extended application and function of PHP parse_ini_file

parse_ini_file ($ filename, $ process_sections = false, $ scanner_mode = INI_SCANNER_NORMAL) parsing a configuration file. filename to resolve the file name; process_sections when set to true, get a multidimensional array, including configuration files in each section names and settings, the default is false; parsing successful return an associative array, else return false. Examples cited at the official website, but also cited the example of the expansion's official website parse_ini_file_multi ().

The following is a profile content:

[first_section]
one = 1
two = 2
name = test

[second_section]
path = '/tmp/test'
url = 'http://test.com/login.php'

[third_section]
php_version[] = '5.0'
php_version[] = '5.1'
php_version[] = '5.5'

[dictionary]
foo[debug] = true
foo[path] = /some/path

[fourth_section]
fold1.fold2.fold3 = 'a'
fold1.fold2.fold4 = 'b'
fold1.fold2.fold5 = 'b'

The following is a PHP file content:

function parse_ini_file_multi($file, $process_sections = false, $scanner_mode = INI_SCANNER_NORMAL){
	$explode_str = '.';
	$escape_char = "'";
	$data = parse_ini_file($file, $process_sections, $scanner_mode);
	if (!$process_sections) {
        $data = array($data);
    }
	foreach ($data as $section_key => $section) {
		foreach($section as $key => $value){
			if(strpos($key, $explode_str)){
				if(substr($key, 0, 1) !== $escape_char){
					$sub_keys = explode($explode_str, $key);
					$subs =& $data[$section_key];
					echo "\r\n".'========='."\r\n";
					print_r($subs);
					print_r($data);
					foreach($sub_keys as $sub_key){
						if (!isset($subs[$sub_key])) {
							$subs[$sub_key] = [];
						}
						$subs =& $subs[$sub_key];
						echo "\r\n".'++++++++'."\r\n";
						print_r($subs);
						print_r($data);
					}
					$subs = $value;
					echo "\r\n".'----------'."\r\n";
					print_r($subs);
					print_r($data);
					unset($data[$section_key][$key]);
				}else{
					$new_key = trim($key, $escape_char);
					$data[$section_key][$new_key] = $value;
					unset($data[$section_key][$key]);
				}
			}
		}
	}
	if (!$process_sections) {
        $data = $data[0];
    }
    return $data;
}

$arr = parse_ini_file('file.ini');
print_r($arr);
echo "\r\n".'================='."\r\n";
$arr = parse_ini_file_multi('file.ini',true);
echo "\r\n".'================='."\r\n";
print_r($arr);

 

Guess you like

Origin blog.csdn.net/uvyoaa/article/details/84339453