Read and write INI files in PHP.

v1.1 2016-08-30 14:29 UTC

This package is not auto-updated.

Last update: 2021-06-19 16:37:35 UTC


README

Read and write INI files/strings in PHP.

Travis branch Packagist Licence HHVM (branch) PHP 5.3.3 or above

Inspired of Piwik/ini

Requirements

  • parse_ini_string is enabled in php.ini

Installation

composer require noczcore/ini

Features

  • Read INI files and INI strings
  • Write INI files and INI strings
  • Return a collection instead of a simple array
  • Throws exceptions instead of PHP errors
  • Better type supports (Exemple):
    • Parse boolean (true/false, on/off, yes/no) to real PHP boolean
    • Parse null to real PHP null
    • Parse int/float to real PHP int/float
  • Advanced parser (Exemple):
    • A key with "[]" has an array value with many values
    • A key with "." has an recursive array value with many values

Usage:

Read

use \NoczCore\Ini\IniReader;
$reader = new IniReader();

$string = <<<INI
[Section 1]
foo = "bar"
number_1 = 1
number_0 = 0
int = 10
float = 10.3
empty = ""
null_ = null
boolean_1 = true
boolean_0 = false
array[] = "string"
array[] = 10.3
array[] = 1
array[] = 0
names.users[] = "NoczCore"
names.administators[] = "John Doe"
names.administators[] = "Jane Doe"
[Section 2]
foo = "bar"
INI;

// Read a string
$array = $reader->readString($string);
// Read a file
$array = $reader->readFile('config.ini');

print_r($array->toArray());

####Return:

Array
(
    [Section 1] => Array
        (
            [foo] => bar
            [number_1] => 1
            [number_0] => 0
            [int] => 10
            [float] => 10.3
            [empty] => ""
            [null_] => null
            [boolean_1] => true
            [boolean_0] => false
            [array] => Array
                (
                    [0] => string
                    [1] => 10.3
                    [2] => 1
                    [3] => 0
                )

            [names] => Array
                (
                    [users] => Array
                        (
                            [0] => NoczCore
                        )

                    [administators] => Array
                        (
                            [0] => John Doe
                            [1] => Jane Doe
                        )

                )

        )

    [Section 2] => Array
        (
            [foo] => bar
        )

)

Write

use \NoczCore\Ini\IniWriter;
$writer = new IniWriter();

// Write to a string
$string = $writer->writeToString($array);
// Write to a file
$writer->writeToFile('config.ini', $array);

License

The Ini component is released under the MIT.

Contributing

To run the unit tests:

vendor/bin/phpunit