PHP code example of iamgerwin / toon-php
1. Go to this page and download the library: Download iamgerwin/toon-php 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/ */
iamgerwin / toon-php example snippets
// JSON: 168 characters, ~42 tokens
{"users":[{"name":"Alice","age":30,"role":"admin"},{"name":"Bob","age":25,"role":"user"}]}
// TOON: 89 characters, ~22 tokens (47% savings!)
users[2]{name,age,role}:
Alice,30,admin
Bob,25,user
use iamgerwin\Toon\Toon;
// Simple encoding
$user = [
'name' => 'Alice',
'email' => '[email protected] ',
'active' => true,
'credits' => 1250
];
$toon = Toon::encode($user);
// Output:
// name: Alice
// email: [email protected]
// active: true
// credits: 1250
// Decode back to PHP
$decoded = Toon::decode($toon);
// Compare with JSON
$comparison = Toon::compare($user);
echo "Token savings: {$comparison['savings_percent']}%";
// Token savings: 42.5%
$users = [
['id' => 1, 'name' => 'Alice', 'role' => 'admin'],
['id' => 2, 'name' => 'Bob', 'role' => 'user'],
['id' => 3, 'name' => 'Charlie', 'role' => 'user'],
];
echo Toon::tabular($users);
// Output:
// [3]{id,name,role}:
// 1,Alice,admin
// 2,Bob,user
// 3,Charlie,user
// vs JSON: {"users":[{"id":1,"name":"Alice"...}]}
$data = ['foo' => 'bar', 'items' => [1, 2, 3]];
// Compact (minimal whitespace)
$compact = Toon::compact($data);
// foo: bar
// items[3]: 1,2,3
// Readable (4-space indentation)
$readable = Toon::readable($data);
// foo: bar
// items[3]: 1,2,3
// Custom options
use iamgerwin\Toon\EncodeOptions;
use iamgerwin\Toon\Enums\ToonDelimiter;
$custom = Toon::encode($data, new EncodeOptions(
indent: 2,
delimiter: ToonDelimiter::TAB,
preferTabular: true
));
$data = ['large' => 'dataset', 'with' => 'many', 'fields' => true];
$analysis = Toon::compare($data);
/*
[
'toon' => ' large: dataset...',
'json' => '{"large":"dataset"...}',
'toon_tokens' => 15,
'json_tokens' => 25,
'savings_percent' => 40.0
]
*/
// Estimate tokens before sending to LLM
$tokens = Toon::estimateTokens($toonString);
echo "Estimated cost: $" . ($tokens / 1000 * 0.03);
// DateTime objects
$data = [
'created_at' => new DateTime('2024-01-01 12:00:00'),
'updated_at' => new DateTime('2024-01-15 10:30:00')
];
$toon = Toon::encode($data);
// created_at: 2024-01-01T12:00:00+00:00
// updated_at: 2024-01-15T10:30:00+00:00
// Enums (PHP 8.1+)
enum Status: string {
case Active = 'active';
case Pending = 'pending';
}
$order = ['status' => Status::Active, 'amount' => 99.99];
$toon = Toon::encode($order);
// status: active
// amount: 99.99
// Nested structures
$complex = [
'user' => [
'profile' => ['name' => 'Alice'],
'settings' => ['theme' => 'dark']
]
];
// Full round-trip support!
// Global helpers for convenience
toon($data); // Encode with defaults
toon_decode($string); // Decode TOON string
toon_compact($data); // Compact format
toon_readable($data); // Readable format
toon_tabular($array); // Tabular for uniform arrays
toon_compare($data); // Compare with JSON
toon_estimate_tokens($string); // Estimate token count
use iamgerwin\Toon\DecodeOptions;
// Strict mode (default) - validates structure
$strict = Toon::decode($toon, DecodeOptions::strict());
// Lenient mode - forgiving parsing
$lenient = Toon::decode($toon, DecodeOptions::lenient());
use iamgerwin\Toon\{EncodeOptions, Enums\ToonDelimiter};
// Use tabs instead of commas
$options = new EncodeOptions(delimiter: ToonDelimiter::TAB);
$toon = Toon::encode($data, $options);
// [3]: value1 value2 value3
// Use pipes
$options = new EncodeOptions(delimiter: ToonDelimiter::PIPE);
$toon = Toon::encode($data, $options);
// [3]: value1|value2|value3
// Building a chatbot with conversation history
$history = [
['role' => 'system', 'content' => 'You are a helpful assistant'],
['role' => 'user', 'content' => 'What is TOON?'],
['role' => 'assistant', 'content' => 'TOON is a token-efficient format...'],
['role' => 'user', 'content' => 'How much can I save?'],
];
// JSON: ~280 tokens = $0.0084 per call
$json = json_encode($history);
// TOON: ~165 tokens = $0.00495 per call (41% savings!)
$toon = Toon::tabular($history);
// Send to OpenAI
$response = $openai->chat()->create([
'model' => 'gpt-4',
'messages' => Toon::decode($toon) // Convert back for API
]);
// Annual savings with 100K calls: $345
// Before (JSON)
$json = json_encode($data);
sendToLLM($json);
$result = json_decode($response);
// After (TOON) - just swap the functions!
$toon = toon($data);
sendToLLM($toon);
$result = toon_decode($response);
// Measure your savings
$comparison = toon_compare($data);
echo "You're now saving {$comparison['savings_percent']}% on tokens!";
bash
composer bash
# PHP 8.1+ (will install v2.x automatically)
composer php:^2.0 # Latest features (PHP 8.1+)
composer