Skip to content

IniHelper

Mistralys edited this page Nov 24, 2019 · 9 revisions

Helper class for reading, modifying and writing INI files, with some features that the native PHP implementation does not offer.

Features

  • When saving, preserve the original ini file formatting (including comments)
  • Duplicate key names support (like the extensions list in php.ini)
  • Easily change and add values
  • Preserve original line ending style
  • Support for single and double quote styles for values

Creating a helper instance

There are three factory methods:

// open an existing ini file $ini = IniHelper::createFromFile('/path/to/file.ini');
$content = "variable=value"; // use an existing INI string $ini = IniHelper::createFromString($content);
// Start with an empty INI helper $ini = IniHelper::createNew();

Reading values

The simplest way is to fetch an array of all values:

$values = $ini->toArray();

Or to fetch the values of a specific section:

if($ini->sectionExists('sectionname')) { $values = $ini->getSection('sectionname')->toArray(); }

Setting values

Single values

Setting or overwriting an existing variable:

$ini->setValue('varname', 'newvalue');

A variable in a section can be set with the path notation using / as separator after the section name:

$ini->setValue('sectionname/varname', 'newvalue');

Note that if the section does not exist, it is automatically added.

Multiple values

When using an array of values as parameter, the variable is automatically converted to duplicate keys:

$ini->setValue('multiple', array('foo', 'bar', 'foobar'));

INI output:

multiple=foo multiple=bar multiple=foobar 

NOTE: If a variable with multiple values already exists, it is overwritten, and only the specified values are kept. In the same way, setting a multiple values variable to a single value deletes all existing values, effectively converting it to a single value variable.

Adding values to duplicate keys

The addValue method can be used to append values to a variable, while keeping the existing values:

INI content:

foo=bar foo=foobar 
$ini->addValue('foo', 'barfoo');

INI output:

foo=bar foo=foobar foo=barfoo 

Saving changes

Changes can be saved either to a file to to a string:

// save to a file, creating it as necessary $ini->saveToFile('/path/to/file.ini');
// get the INI file contents as string $content = $ini->saveToString();

It is also possible to get the INI string for a single section:

if($ini->sectionExists('sectionname')) { $content = $ini->getSection('sectionname')->toString(); }

INI formatting

Values quote style

Values can be quoted, but do not have to be. The only time you must use quotes is if you wish to preserve whitespace.

Quote style can be either single quotes or double quotes. Example:

foo=" bar " bar=' foo ' 

Section names

The naming of section names is entirely free, including PHP keywords. There are only two restrictions:

  • They cannot contain slashes, as these are used in variable paths.
  • Any whitespace around the name is automatically trimmed.

New here?

Have a look at the overview for a list of all helper classes available in the package.

Table of contents

Find the current page in the collapsible "Pages" list above, and expand the page, to view a table of contents.

Clone this wiki locally