Giter Club home page Giter Club logo

iniparser's Introduction

IniParser

IniParser is a really simple parser for "complex" INI files.

Examples

Standard INI files look like this:

key = value
another_key = another value

[section_name]
a_sub_key = yet another value

And when parsed with PHP's built-in parse_ini_string() or parse_ini_file(), looks like

array(
    'key' => 'value',
    'another_key' => 'another value',
    'section_name' => array(
        'a_sub_key' => 'yet another value'
    )
)

Complex example

This is great when you just want a super simple configuration file, but here is a super-charged INI file that you might find in the wild:

environment = testing

[testing]
debug = true
database.connection = "mysql:host=127.0.0.1"
database.name = test
database.username = 
database.password =
secrets = [1,2,3]

[staging : testing]
database.name = stage
database.username = staging
database.password = 12345

[production : staging]
debug = false;
database.name = production
database.username = root

And when parsed with IniParser:

array(
    'environment' => 'testing',
    'testing' => array(
        'debug' => '1',
        'database' => array(
            'connection' => 'mysql:host=127.0.0.1',
            'name' => 'test',
            'username' => '',
            'password' => ''
        ),
        'secrets' => array('1','2','3')
    ),
    'staging' => array(
        'debug' => '1',
        'database' => array(
            'connection' => 'mysql:host=127.0.0.1',
            'name' => 'stage',
            'username' => 'staging',
            'password' => '12345'
        ),
        'secrets' => array('1','2','3')
    ),
    'production' => array(
        'debug' => '',
        'database' => array(
            'connection' => 'mysql:host=127.0.0.1',
            'name' => 'production',
            'username' => 'root',
            'password' => '12345'
        ),
        'secrets' => array('1','2','3')
    )
)

Additional Features

As you can see, IniParser supports section inheritance with [child : parent], property nesting with a.b.c = d, and simple arrays with [a,b,c].

Additionally, you can sub-class IniParser and override the parse_key() and parse_value() methods to customize how it parses keys and values.

ArrayObject

As an added bonus, IniParser also allows you to access the values OO-style:

echo $config->production->database->connection; // output: mysql:host=127.0.0.1
echo $config->staging->debug; // output: 1

iniparser's People

Contributors

austinhyde avatar till avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.