1. Go to this page and download the library: Download petalbranch/toml 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/ */
petalbranch / toml example snippets
use Petalbranch\Toml\Toml;
Toml::enableDetailedErrors(false); // 全局关闭详细错误输出
use Petalbranch\Toml\Parser\Lexer;
use Petalbranch\Toml\Parser\Parser;
use Petalbranch\Toml\Dumper\NodeDumper;
use Petalbranch\Toml\Model\DumperConfig;
$originalToml = <<<TOML
# 数据库核心配置
[database]
server = "192.168.1.1" # 主库 IP
port = 3306 # 请勿在生产环境修改
TOML;
// 1. 解析为包含完整注释的 AST 树
$parser = new Parser(new Lexer());
$rootNode = $parser->parseToNode($originalToml);
// 2. 直接修改 AST 节点的值 (自动推断类型)
$rootNode->get('database')->get('port')->setValue(6379);
// 3. 拿着这棵树直接 Dump 出去
$dumper = new NodeDumper(new DumperConfig());
echo $dumper->dump($rootNode);
/* 输出结果:注释被完美保留!
# 数据库核心配置
[database]
server = "192.168.1.1" # 主库 IP
port = 6379 # 请勿在生产环境修改
*/