PHPackages                             kayzorelabs/phpconfigparser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. kayzorelabs/phpconfigparser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

kayzorelabs/phpconfigparser
===========================

A configuration file parser for PHP 7.1 and up

1.1.0(7y ago)112[1 issues](https://github.com/kayzorelabs/phpconfigparser/issues)LGPL-3.0PHPPHP &gt;=7.1

Since Nov 24Pushed 7y agoCompare

[ Source](https://github.com/kayzorelabs/phpconfigparser)[ Packagist](https://packagist.org/packages/kayzorelabs/phpconfigparser)[ Docs](https://github.com/kayzorelabs/phpconfigparser)[ RSS](/packages/kayzorelabs-phpconfigparser/feed)WikiDiscussions master Synced 5d ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

ConfigParser - A Configuration File Parser for PHP 7.1 and up
=============================================================

[](#configparser---a-configuration-file-parser-for-php-71-and-up)

[![Build Status](https://camo.githubusercontent.com/4b4f130947e62ddca9171020697af725e68475781c87ea80425def67aa73c777/68747470733a2f2f7472617669732d63692e6f72672f6b61797a6f72656c6162732f706870636f6e6669677061727365722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/kayzorelabs/phpconfigparser)

What is ConfigParser?
---------------------

[](#what-is-configparser)

ConfigParser is a configuration file parser for PHP 7.1 and up. It's a copy of [NoiseLabs ConfigParser](https://github.com/noiselabs/configparser.php), but with updated code.

The ConfigParser class provides a way to read, interpret and write configuration files with structure similar to what’s found in Microsoft Windows INI files.

Requirements
============

[](#requirements)

- PHP 7.1 and up.

License
=======

[](#license)

ConfigParser is licensed under the LGPLv3 License. See the LICENSE file for details.

Installation (Composer)
=======================

[](#installation-composer)

### 0. Install Composer

[](#0-install-composer)

If you don't have Composer yet, download it following the instructions on  or just run the following command:

```
curl -s http://getcomposer.org/installer | php
```

### 1. Add the kayzorelabs/phpconfigparser package in your project

[](#1-add-the-kayzorelabsphpconfigparser-package-in-your-project)

Run composer require command

```
composer.phar require kayzorelabs/phpconfigparser

```

Or add the kayzore/configparser package in your composer.json

```
{
    "require": {
        "kayzorelabs/phpconfigparser": "dev-master"
    }
}
```

And tell composer to download the package by running the command:

```
$ php composer.phar update kayzorelabs/phpconfigparser
```

Composer will install the bundle to your project's `vendor/kayzorelabs` directory.

Documentation
=============

[](#documentation)

Basic instructions on the usage of the library are presented below.

Supported INI File Structure
----------------------------

[](#supported-ini-file-structure)

A configuration file consists of sections, each led by a `[section]` header, followed by `name = value` entries..

Leading and trailing whitespace is removed from keys and values. Values can be omitted, these will be stored as an empty string.

Configuration files may include comments, prefixed by `;`. Hash marks (`#` ) may no longer be used as comments and will throw a deprecation warning if used.

Usage
-----

[](#usage)

First, take the following INI file as an example:

```
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[github.com]
user = foo

[topsecret.server.com]
Port = 50022
ForwardX11 = no

```

Using ConfigParser is as simples as:

```

```

### Using ConfigParser like an associative array

[](#using-configparser-like-an-associative-array)

Because it implements `ArrayAccess` the ConfigParser object can be used in a straightforward way:

```
$cfg = new ConfigParser();

$cfg->read('/home/user/.config/server.cfg');

// get values
echo $cfg['github.com']['user'];

// set options for the 'github.com' section
$cfg['github.com'] = array('user', 'bar');

?>

```

### Iterate

[](#iterate)

And because ConfigParser implements `IteratorAggregate` it is also possible to use `foreach` to loop over the configuration.

```
$cfg = new ConfigParser();

foreach ($cfg as $section => $name) {
    echo sprintf("Section '%s' has the following options: %s\n",
                $section,
                implode(", ", $cfg->options($section))
                );
}

```

### Loading multiple files at once

[](#loading-multiple-files-at-once)

This is designed so that you can specify a list of potential configuration file locations (for example, the current directory, the user’s home directory, and some system-wide directory), and all existing configuration files in the array will be read.

```
$cfg = new ConfigParser();

$cfg->read(array('/etc/myapp.cfg', '/usr/local/etc/myapp.cfg', '/home/user/.config/myapp.cfg');

```

### Parsing files without sections

[](#parsing-files-without-sections)

ConfigParser was designed to work with INI files with section tags. For simple files with just `option = value` entries `NoSectionsConfigParser` can be used.

```

```

### Supported Datatypes

[](#supported-datatypes)

ConfigParser do not guess datatypes of values in configuration files, always storing them internally as strings. This allows reading entries like `pager = false` and keeping values as it is (without any kind of boolean parsing).

This means that if you need other datatypes, you should convert on your own, or use one of these methods:

- Integers:

    ```
      $cfg->getInt('topsecret.server.com', 'Port');

    ```
- Floats:

    ```
      $cfg->getFloat('topsecret.server.com', 'CompressionLevel');

    ```
- Booleans:

    ```
      $cfg->getBoolean('topsecret.server.com', 'ForwardX11');

    ```

### Fallback Values

[](#fallback-values)

When using `get()` to pull a value from the configuration you may provide a fallback value in case that option doesn't exist.

```
// API: ConfigParser::get($section, $option, $fallback)

```

Please note that default values have precedence over fallback values. For instance, in our example the 'CompressionLevel' key was specified only in the 'DEFAULT' section. If we try to get it from the section 'topsecret.server.com', we will always get the default, even if we specify a fallback:

```
echo $cfg->get('topsecret.server.com', 'CompressionLevel', '3');
// prints 9

```

Customizing Parser Behaviour
----------------------------

[](#customizing-parser-behaviour)

### Loading a set of default options/values

[](#loading-a-set-of-default-optionsvalues)

You may create an array of key-value pairs and pass them to the constructor as the first argument. These option/values will be initially put in the DEFAULT section. This makes for an elegant way to support concise configuration files that don’t specify values which are the same as the documented default.

```
// define some defaults values
$defaults = array(
                'Compression' 		=> 'yes',
                'CompressionLevel' 	=> 9
                );

$cfg = new ConfigParser($defaults);

```

### Advanced configuration

[](#advanced-configuration)

ConfigParser includes a small set of internal options to change the way it writes to configuration files or if exceptions are to be raised.

- **delimiter** - The delimiter character to use between keys and values (when writing). Defaults to `= `.
- **space\_around\_delimiters** - Inserts (or not) a blank space between keys/values and delimiters. Defaults to `TRUE`.
- **linebreak** - The linebreak to use. Defaults to `'\r\n'` on Windows OS and `'\n'` on every other OS (Linux, Mac).
- **throw\_exceptions** - Use this option to disable exceptions. If set to false ConfigParser will write to the error log instead. Defaults to `TRUE`.

### Using a custom error logger

[](#using-a-custom-error-logger)

If you have disabled PHP exceptions (see above section) ConfigParser will use `error_log()` to record the exception message. In this scenario you may want to use a custom error logger instead of `error_log`, or even disable logging at all.

To override the original logger method just extend ConfigParser and replace `ConfigParser::log()` with your own implementation.

[Monolog](https://github.com/Seldaek/monolog) is a great logging library for PHP 5.3 and will be used as our custom logger in the following example.

```

```

Development
===========

[](#development)

Original Authors
----------------

[](#original-authors)

- Vítor Brandão -  / [twitter](http://twitter.com/noiselabs) / [blog](http://blog.noiselabs.org)

Update Authors
--------------

[](#update-authors)

- Kayzore Labs -

Submitting bugs and feature requests
------------------------------------

[](#submitting-bugs-and-feature-requests)

Bugs and feature requests are tracked on [GitHub](https://github.com/kayzorelabs/phpconfigparser/issues).

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

2729d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/de9bee290fb94b6049228948913c9adda270a078c880e26c74b3c765278d3175?d=identicon)[kayzorelabs](/maintainers/kayzorelabs)

---

Tags

configparsernoiselabsconfigparserkayzorelabs

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kayzorelabs-phpconfigparser/health.svg)

```
[![Health](https://phpackages.com/badges/kayzorelabs-phpconfigparser/health.svg)](https://phpackages.com/packages/kayzorelabs-phpconfigparser)
```

###  Alternatives

[nikic/php-parser

A PHP parser written in PHP

17.4k902.6M1.8k](/packages/nikic-php-parser)[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[erusev/parsedown

Parser for Markdown.

15.0k151.8M732](/packages/erusev-parsedown)[league/commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)

2.9k404.0M702](/packages/league-commonmark)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
