A modern, fully-typed PHP library for parsing and generating spintax text variations. Works with any PHP framework or standalone.
Spintax (spin syntax) represents multiple text variations using curly braces and pipes:
{Hello|Hi|Hey} {world|universe}!
Generates: "Hello world!", "Hi universe!", "Hey world!", etc.
composer require braseidon/spintaxRequirements: PHP 8.1 or higher
use Braseidon\Spintax\Spintax;
// Generate a random variation
$text = Spintax::spin('{Hello|Hi} {world|universe}!');
// → "Hello world!" or "Hi universe!" etc.
// Get all possible variations
$variations = Spintax::allVariations('{yes|no}');
// → ['yes', 'no']
// Count variations without generating them
$count = Spintax::countVariations('{a|b|c} {1|2}');
// → 6 (3 × 2 combinations)
// Generate using a specific path
$text = Spintax::replicate('{hello|hi}', [0]);
// → "hello"- ✅ Modern PHP 8.1+ - Full type safety with strict types
- ✅ Zero Dependencies - Works with Laravel, Symfony, WordPress, or standalone
- ✅ Nested Spintax -
{The {quick|fast} {brown|red} fox|A lazy dog} - ✅ Exception Handling - Validates balanced braces with clear error messages
- ✅ Comprehensive Tests - 27 tests, 52 assertions, 100% passing
- ✅ PSR-4 Autoloading - Standard Composer package
$template = '{Welcome|Hello} back, {friend|user}!';
echo Spintax::spin($template);
// → "Welcome back, friend!" (random)$template = '{yes|no|maybe}';
$all = Spintax::allVariations($template);
// → ['yes', 'no', 'maybe']$template = '{a|b} {1|2} {x|y}';
$count = Spintax::countVariations($template);
// → 8 (2 × 2 × 2)$template = '{The {quick|fast} {brown|red} fox|A lazy dog}';
echo Spintax::spin($template);
// → "The quick brown fox" or "A lazy dog"$template = 'This is{| very| extremely} cool';
echo Spintax::spin($template);
// → "This is cool" or "This is very cool"// Generate same variation for specific user
$template = Spintax::parse('{Hello|Hi}, {friend|buddy}!');
// Use specific path for reproducibility
echo Spintax::replicate($template, [0, 1]);
// → Always "Hello, buddy!"
// Or use path string
echo Spintax::replicate($template, '1,0');
// → Always "Hi, friend!"$node = Spintax::parse('{a|b|c} {1|2}');
// Count combinations
echo count($node); // → 6
// Get all possible paths
$paths = $node->getPaths();
// → [[0,0], [0,1], [1,0], [1,1], [2,0], [2,1]]
// Dump back to spintax
echo Spintax::dump($node); // → "{a|b|c} {1|2}"Generate a random variation from spintax text.
Parse spintax into a tree structure for advanced manipulation.
Generate text using a specific path.
- Array path:
[0, 1, 0]- index of choice at each branch - String path:
"0,1,0"- comma-separated indices
Get all possible text variations.
Count variations without generating them.
Convert a parsed tree back to spintax format.
Represents a node in the spintax tree. Get instances via Spintax::parse().
Methods:
generate(array &$path = []): string- Generate textdump(): string- Convert to spintax formatgetPaths(): array- Get all possible pathscount(): int- Count variations (Countable)getContent(): string- Get node's textgetChildren(): array- Get child alternativesgetNext(): ?SpintaxNode- Get next sequential nodehasChildren(): bool- Check for alternativeshasNext(): bool- Check for next node
Lower-level parsing and generation. Usually use Spintax facade instead.
Methods:
parse(string $input): SpintaxNodegenerate(string|SpintaxNode $content): stringreplicate(string|SpintaxNode $content, array|string $path): string
The library validates spintax syntax and throws InvalidArgumentException for malformed input:
try {
Spintax::spin('{unclosed brace');
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
// → "Unbalanced braces in spintax input: unclosed opening brace"
}$article = "Start by {leveling|grinding} in {Act 1|the first zone}. " .
"{Focus on|Prioritize} {damage|survivability}.";
foreach (Spintax::allVariations($article) as $variation) {
// Generate multiple unique articles
}$headline = '{Try|Start} {now|today} for {free|$0}!';
$variant = Spintax::spin($headline);$greeting = '{Hi|Hello|Hey} {there|friend},';
$user_greeting = Spintax::spin($greeting);$meta = 'Shop {affordable|cheap|budget} {shoes|footwear} {online|on the web}';
$description = Spintax::spin($meta);composer testAll 27 tests pass with 52 assertions.
Works with any PHP project:
- ✅ Laravel 8, 9, 10, 11, 12+
- ✅ Symfony 5, 6, 7+
- ✅ WordPress
- ✅ CodeIgniter, Yii, CakePHP
- ✅ Standalone PHP scripts
No framework-specific code or dependencies!
Efficient tree-based parsing with minimal memory usage:
- Handles deeply nested spintax
- Counts variations without generating them
- Fast random generation
Based on the ChillDev Spintax library by Rafał Wrzeszcz (2014).
Completely modernized and rewritten for PHP 8.4+ in 2025 with:
- Full type declarations
- Exception handling
- Improved API
- Comprehensive tests
- Modern PHP patterns
MIT License - see LICENSE file for details.
Contributions welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
- Issues: GitHub Issues
- Docs: This README
- Tests: See
tests/directory for examples