PHP code example of sweetchuck / ini-serializer

1. Go to this page and download the library: Download sweetchuck/ini-serializer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sweetchuck / ini-serializer example snippets


$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']);
}

use Sweetchuck\IniSerializer\IniSerializer;

$ini = <<<INI
[database]
host=localhost
port=3306
debug=true
password=

[cache]
enabled=yes
ttl=3600
max_size=null
INI;

$serializer = new IniSerializer();
$data = $serializer->parse($ini);

// Result:
// [
//     'database' => [
//         'host' => 'localhost',
//         'port' => 3306,
//         'debug' => true,
//         'password' => null,
//     ],
//     'cache' => [
//         'enabled' => true,
//         'ttl' => 3600,
//         'max_size' => null,
//     ],
// ]

$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
//

$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"
//

$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]

$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);