PHPackages                             sweetchuck/ini-serializer - 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. sweetchuck/ini-serializer

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

sweetchuck/ini-serializer
=========================

INI reader-writer

v2.0.0(7mo ago)13361GPL-2.0-or-laterPHPPHP &gt;=8.4CI passing

Since Oct 16Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/Sweetchuck/ini-serializer)[ Packagist](https://packagist.org/packages/sweetchuck/ini-serializer)[ RSS](/packages/sweetchuck-ini-serializer/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (2)Dependencies (11)Versions (8)Used By (0)

INI encode/decode
=================

[](#ini-encodedecode)

[![QA](https://github.com/Sweetchuck/ini-serializer/actions/workflows/qa.yml/badge.svg?branch=2.x)](https://github.com/Sweetchuck/ini-serializer/actions/workflows/qa.yml)[![codecov](https://camo.githubusercontent.com/ae96f1cadcfc06d46873e6591724b0ce1ea9093a1baa378ba40616a788478175/68747470733a2f2f636f6465636f762e696f2f67682f5377656574636875636b2f696e692d73657269616c697a65722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/Sweetchuck/ini-serializer)

A flexible PHP library for parsing and generating INI files with flexible type handling, customizable formatting options, and full control over serialization behavior.

Features
--------

[](#features)

- **Intelligent Type Conversion**: Automatically converts values to proper PHP types (integers, floats, booleans, null)
- **Customizable Type Aliases**: Define your own values for `null`, `true`, and `false`
- **Comment Support**: Handles both `;` and `#` style comments (configurable)
- **Grouped Sections**: Full support for INI sections `[group_name]`
- **Flexible Formatting**: Control quote usage and spacing around equal signs
- **Bidirectional**: Parse INI strings to PHP arrays and emit PHP arrays back to INI format
- **UTF-8 Ready**: Built-in mbstring support for proper Unicode handling

Why Use This Over PHP's Native `parse_ini_file()`?
--------------------------------------------------

[](#why-use-this-over-phps-native-parse_ini_file)

### 1. Predictable Type Conversion

[](#1-predictable-type-conversion)

- This library explicitly converts typed values (bool, null, int, float)
- You can configure which string values represent `true`, `false`, and `null`

### 2. Complete Bidirectional Support

[](#2-complete-bidirectional-support)

- Native `parse_ini_file()` only reads INI files
- This library can generate INI files from PHP arrays, maintaining structure

### 3. Formatting Control

[](#3-formatting-control)

- Configure spacing around equal signs
- Control whether strings are quoted
- Generate consistently formatted INI files

### 4. Customizable Comment Characters

[](#4-customizable-comment-characters)

- Change which characters are treated as comments
- Useful for different INI dialects

### 5. Better Boolean Handling

[](#5-better-boolean-handling)

- Recognizes multiple boolean representations:
    - True: `true`, `on`, `yes`
    - False: `false`, `off`, `no`
- All case-insensitive

### 6. Proper Null Handling

[](#6-proper-null-handling)

- Distinguishes between empty strings and null values
- Recognizes `null` and `nil` as null values

### 7. Group Name Escaping

[](#7-group-name-escaping)

- Properly handles special characters in section names `[group_name]`

Limitations
-----------

[](#limitations)

⚠️ **Important**: This library does **not** resolve PHP constants or environment variables.

- PHP constants like `\PHP_VERSION` or `\E_ALL` are treated as regular strings
- Environment variables are not interpolated during parsing
- If you need constant/environment variable support, you should handle that separately before parsing or after parsing

Example:

```
$ini = 'debug=DEBUG_MODE'; // DEBUG_MODE constant won't be resolved
$data = $serializer->parse($ini);
$data['debug']; // Returns string 'DEBUG_MODE', not the constant value

// If you need this, resolve it manually:
if (defined($data['debug'])) {
    $data['debug'] = constant($data['debug']);
}
```

Usage
-----

[](#usage)

### Basic Parsing

[](#basic-parsing)

```
use Sweetchuck\IniSerializer\IniSerializer;

$ini =  'localhost',
//         'port' => 3306,
//         'debug' => true,
//         'password' => null,
//     ],
//     'cache' => [
//         'enabled' => true,
//         'ttl' => 3600,
//         'max_size' => null,
//     ],
// ]
```

### Generating INI Files

[](#generating-ini-files)

```
$data = [
    'database' => [
        'host' => 'localhost',
        'port' => 3306,
        'debug' => true,
        'timeout' => 30.5,
    ],
    'cache' => [
        'enabled' => false,
        'ttl' => 3600,
    ],
];

$serializer = new IniSerializer();
$iniString = $serializer->emit($data);

echo $iniString;
// Output:
// [database]
// host=localhost
// port=3306
// debug=true
// timeout=30.5
//
// [cache]
// enabled=false
// ttl=3600
//
```

### Formatting Options

[](#formatting-options)

```
$serializer = new IniSerializer();

// Add spaces around equal signs
$serializer->setSpaceAroundEqualSign(true);

// Quote all string values
$serializer->setQuoteStrings(true);

$data = ['section' => ['key' => 'value']];
$ini = $serializer->emit($data);

// Output:
// [section]
// key = "value"
//
```

### Customizing Type Values

[](#customizing-type-values)

```
$serializer = new IniSerializer();

// Customize what values represent booleans
$serializer->setValueBoolTrue(['yes', 'on', 'enabled', '1']);
$serializer->setValueBoolFalse(['no', 'off', 'disabled', '0']);

// Customize what values represent null
$serializer->setValueNull(['null', 'nil', 'none']);

// Or set all options at once
$serializer->setOptions([
    'valueBoolTrue' => ['yes', 'on', '1'],
    'valueBoolFalse' => ['no', 'off', '0'],
    'valueNull' => ['null', 'nil'],
    'commentChars' => [';', '#'],
    'quoteStrings' => false,
    'spaceAroundEqualSign' => false,
]);

$iniString = 'enabled=yes
is_prod=no
api_key=null';

$data = $serializer->parse($iniString);
// Result: ['enabled' => true, 'is_prod' => false, 'api_key' => null]
```

### Working with Existing Files

[](#working-with-existing-files)

```
$serializer = new IniSerializer();

// Read and parse
$iniContent = file_get_contents('config.ini');
$config = $serializer->parse($iniContent);

// Modify
$config['database']['host'] = 'newhost.com';

// Write back
$updatedIni = $serializer->emit($config);
file_put_contents('config.ini', $updatedIni);
```

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance73

Regular maintenance activity

Popularity19

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 85% of commits — single point of failure

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 ~36 days

Total

4

Last Release

106d ago

Major Versions

v1.0.0 → v2.0.02025-10-16

PHP version history (2 changes)v1.0.0PHP &gt;=7.4

v2.0.0PHP &gt;=8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/591103?v=4)[Andor](/maintainers/Sweetchuck)[@Sweetchuck](https://github.com/Sweetchuck)

---

Top Contributors

[![Sweetchuck](https://avatars.githubusercontent.com/u/591103?v=4)](https://github.com/Sweetchuck "Sweetchuck (17 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")

---

Tags

serializerini

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sweetchuck-ini-serializer/health.svg)

```
[![Health](https://phpackages.com/badges/sweetchuck-ini-serializer/health.svg)](https://phpackages.com/packages/sweetchuck-ini-serializer)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[goetas-webservices/xsd2php

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata

2411.6M37](/packages/goetas-webservices-xsd2php)[goetas-webservices/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

4910.9M36](/packages/goetas-webservices-xsd2php-runtime)[flix-tech/avro-serde-php

A library to serialize and deserialize Avro records making use of the confluent schema registry

674.0M17](/packages/flix-tech-avro-serde-php)[laminas/laminas-serializer

Serialize and deserialize PHP structures to a variety of representations

3411.2M115](/packages/laminas-laminas-serializer)

PHPackages © 2026

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