Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing an "advanced" INI file with PHP

Tags:

php

parsing

ini

Basically, I would like a simple, easy, one-file way to parse an INI file with "advanced" features, like section inheritance and property nesting, like Zend_Config_Ini.

For example:

[foo]
a = 1
b.a = 2
b.b = 3
b.c = 4
c = 5

[bar : foo]
b.b = 17
c = 42

Would parse into

array(
  'foo'=>array(
    'a'=>'1',
    'b'=>array(
      'a'=>'2',
      'b'=>'3',
      'c'=>'4'
    ),
    'c'=>'5'
  ),
  'bar'=>array(
    'a'=>'1',
    'b'=>array(
      'a'=>'2',
      'b'=>'17',
      'c'=>'4'
    ),
    'c'=>'42'
  )
)

PHP's built-in parse_ini_file, doesn't handle anything other than simple INI's with simple sections and simple keys.

My problem with using Zend_Config_Ini is that I would have to include virtually the whole Zend_Config subpackage, and is super-bloated and configurable.

Is there a small and simple library available to parse this?
If not, is there an easy implementation I'm not seeing?

By small and simple, I mean something like the sfYaml of INI files.

To my (very inexperienced) eyes, I would have to parse through once with parse_ini_file, then come back and resolve inheritance, then run through each section and expand the keys recursively...

UPDATE: Since this seems to be a popular question, I would like to note that I have a simple class implementing this on GitHub, feel free to send pull requests, issues, etc.

like image 662
Austin Hyde Avatar asked Jul 13 '10 22:07

Austin Hyde


People also ask

Which ini file is PHP using?

user. ini file is the default configuration file for running applications that require PHP. It is used to control variables such as upload sizes, file timeouts, and resource limits. This file is located on your server in the /public_html folder.

What is Parse_ini_file?

Definition and Usage. The parse_ini_file() function parses a configuration (ini) file and returns the settings. Tip: This function can be used to read in your own configuration files, and has nothing to do with the php. ini file.

What should be in my PHP ini file?

The php. ini file contains all of the current PHP configuration settings: such as the execution time, memory limit, etc. This is also how PECL modules are enabled such as memcache, APC, etc. This file allows you to override the server's default configuration settings.


1 Answers

Not sure if I should edit my old answer or add a new one.

Try this version of it, should be what you're looking for.

function parse_ini_advanced($array) {
    $returnArray = array();
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            $e = explode(':', $key);
            if (!empty($e[1])) {
                $x = array();
                foreach ($e as $tk => $tv) {
                    $x[$tk] = trim($tv);
                }
                $x = array_reverse($x, true);
                foreach ($x as $k => $v) {
                    $c = $x[0];
                    if (empty($returnArray[$c])) {
                        $returnArray[$c] = array();
                    }
                    if (isset($returnArray[$x[1]])) {
                        $returnArray[$c] = array_merge($returnArray[$c], $returnArray[$x[1]]);
                    }
                    if ($k === 0) {
                        $returnArray[$c] = array_merge($returnArray[$c], $array[$key]);
                    }
                }
            } else {
                $returnArray[$key] = $array[$key];
            }
        }
    }
    return $returnArray;
}
function recursive_parse($array)
{
    $returnArray = array();
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $array[$key] = recursive_parse($value);
            }
            $x = explode('.', $key);
            if (!empty($x[1])) {
                $x = array_reverse($x, true);
                if (isset($returnArray[$key])) {
                    unset($returnArray[$key]);
                }
                if (!isset($returnArray[$x[0]])) {
                    $returnArray[$x[0]] = array();
                }
                $first = true;
                foreach ($x as $k => $v) {
                    if ($first === true) {
                        $b = $array[$key];
                        $first = false;
                    }
                    $b = array($v => $b);
                }
                $returnArray[$x[0]] = array_merge_recursive($returnArray[$x[0]], $b[$x[0]]);
            } else {
                $returnArray[$key] = $array[$key];
            }
        }
    }
    return $returnArray;
}

Would be called like this:

$array = parse_ini_file('test.ini', true);
$array = recursive_parse(parse_ini_advanced($array));

This could be done a lot better/clearer but for a simple solution it should work just fine.

If your config is:

[foo]
a = 1
b.a = 2
b.b = 3
b.c = 4
c = 5

[bar : foo]
b.x.c = 33
b.b = 17
c = 42

[hot : bar : foo]
b.a = 83
b.d = 23

The output should be:

Array
(
[foo] => Array
    (
        [a] => 1
        [b] => Array
            (
                [a] => 2
                [b] => 3
                [c] => 4
            )

        [c] => 5
    )

[bar] => Array
    (
        [a] => 1
        [b] => Array
            (
                [a] => 2
                [b] => 17
                [c] => 4
                [x] => Array
                    (
                        [c] => 33
                    )

            )

        [c] => 42
    )

[hot] => Array
    (
        [a] => 1
        [b] => Array
            (
                [a] => 83
                [b] => 17
                [c] => 4
                [x] => Array
                    (
                        [c] => 33
                    )

                [d] => 23
            )

        [c] => 42
    )
)
like image 194
Viper_Sb Avatar answered Sep 18 '22 16:09

Viper_Sb