Skip to content
A PHP parser for TOML
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Some methods have become protected Aug 8, 2018
.travis.yml Added the release date of the version 1.0.4 Aug 8, 2018
composer.json Package yosymfony/parser-utils has been updated to 2.0.0 Jun 29, 2018

TOML parser for PHP

A PHP parser for TOML compatible with TOML v0.4.0.

Build Status Latest Stable Version Total Downloads




Requires PHP >= 7.1.

Use Composer to install this package:

composer require yosymfony/toml


You can parse an inline TOML string or from a file:

To parse an inline TOML string:

use Yosymfony\Toml\Toml;

$array = Toml::Parse('key = [1,2,3]');


To parse a TOML file:

$array = Toml::ParseFile('example.toml');


Additionally, methods parse and parseFile accept a second argument called resultAsObject to return the result as an object based on stdClass.

$object = Toml::Parse('key = [1,2,3]', true);


You can create a TOML string with TomlBuilder. TomlBuilder uses a fluent interface for more readable code:

    use Yosymfony\Toml\TomlBuilder;

    $tb = new TomlBuilder();

    $result = $tb->addComment('Toml file')
        ->addValue('name', "Toml", 'This is your name')
        ->addValue('newline', "This string has a \n new line character.")
        ->addValue('winPath', "C:\\Users\\nodejs\\templates")
        ->addValue('literal', '@<\i\c*\s*>') // literals starts with '@'.
        ->addValue('unicode', 'unicode character: ' . json_decode('"\u03B4"'))

        ->addValue('t', true)
        ->addValue('f', false)

        ->addValue('positive', 25, 'Comment inline.')
        ->addValue('negative', -25)

        ->addValue('positive', 25.25)
        ->addValue('negative', -25.25)

        ->addValue('datetime', new \Datetime())

        ->addComment('Related to arrays')

        ->addValue('simple', array(1,2,3))
        ->addValue('multiple', array(
            array('abc', 'def'),
            array(1.1, 1.2),
            array(true, false),
            array( new \Datetime()) ))

        ->addComment('Array of tables')

        ->addArrayOfTable('fruit')                            // Row
            ->addValue('name', 'apple')
                ->addValue('name', 'red delicious')
                ->addValue('name', 'granny smith')
        ->addArrayOfTable('fruit')                            // Row
            ->addValue('name', 'banana')
                ->addValue('name', 'plantain')
        ->getTomlString();    // Generate the TOML string

The result:

#Toml file

name = "Toml" #This is your name
newline = "This string has a \n new line character."
winPath = "C:\\Users\\nodejs\\templates"
literal = '<\i\c*\s*>'
unicode = "unicode character: δ"

t = true
f = false

positive = 25 #Comment inline.
negative = -25

positive = 25.25
negative = -25.25

datetime = 2013-06-10T21:12:48Z

#Related to arrays

simple = [1, 2, 3]
multiple = [[1, 2], ["abc", "def"], [1.1, 1.2], [true, false], [2013-06-10T21:12:48Z]]

# Array of tables

    name = "apple"

        name = "red delicious"

        name = "granny smith"

    name = "banana"

    name = "plantain"


The TomlBuilder class is an utility to get Toml strings that has the following limitations:

  • Only admits basic strings and literal strings.

Deprecated method

The following method will be eliminated in version 2.0.0

  • [TomlBuilder] addArrayTables


When Contributing code to this library, you must follow its coding standards. Toml follows PSR-2 coding style. To ensure the CS, you can use the CLI tool PHP-CS-Fixer.

Unit tests

You can run the unit tests with the following command:

$ cd toml
$ composer test


This library is open-sourced software licensed under the MIT license.

You can’t perform that action at this time.