mdwheele/zalgo
Generate “Zalgo” glitch text in PHP/Laravel by corrupting strings with combining diacritics. Install via Composer, create a Soul, then summon a Zalgo instance with a Mood (e.g., soothed) to make text speak with chaotic effects.
Installation:
composer require mdwheele/zalgo
Add to composer.json under require-dev if only for testing.
First Use Case:
use Zalgo\Zalgo;
$text = "Hello, world!";
$zalgo = new Zalgo();
echo $zalgo->apply($text); // Outputs: H̫̺̳e̬̜͎l̖̗̻̣̹̕l̴̘̝̱̰̠̩o̶̤̮̩̘,̨̻̪̖͔ ̣̭w̮̲̝̼̩̝͖o̸̰̩̖ͅr̞̘̫̩̼l̡͍̬͎̪̺͚͔d̢͓̪͕̜̰̠̦!
Where to Look First:
Zalgo\Zalgo (main class with apply() method)./tests/ for edge cases (e.g., empty strings, special chars).Basic Usage:
$zalgo = new Zalgo();
$result = $zalgo->apply($inputText);
Integration with Laravel:
View Helpers:
// app/Helpers/ZalgoHelper.php
if (!function_exists('zalgo')) {
function zalgo($text) {
return app(Zalgo\Zalgo::class)->apply($text);
}
}
Use in Blade:
{{ zalgo('Your text here') }}
Middleware (for fun):
namespace App\Http\Middleware;
use Zalgo\Zalgo;
class ZalgoMiddleware {
public function handle($request, Closure $next) {
$response = $next($request);
$response->setContent(
app(Zalgo::class)->apply($response->getContent())
);
return $response;
}
}
Register in app/Http/Kernel.php under $routeMiddleware.
Conditional Application:
$zalgo = new Zalgo();
$shouldApply = true; // e.g., from config or user input
echo $shouldApply ? $zalgo->apply($text) : $text;
Testing:
use Zalgo\Zalgo;
use PHPUnit\Framework\TestCase;
class ZalgoTest extends TestCase {
public function testApply() {
$zalgo = new Zalgo();
$result = $zalgo->apply("Test");
$this->assertNotEquals("Test", $result);
$this->assertStringContainsString("̫̺̳", $result);
}
}
Performance:
$cachedZalgo = cache()->remember("zalgo_{$text}", now()->addHours(1), function() use ($text, $zalgo) {
return $zalgo->apply($text);
});
HTML/UTF-8 Issues:
$cleanHtml = strip_tags($zalgo->apply($dirtyHtml));
Database Storage:
Randomness:
Zalgo class in tests to return predictable output.Inspect Output:
Use htmlentities() to visualize diacritics in logs:
error_log(htmlentities($zalgo->apply("Test")));
Disable Zalgo Temporarily: Extend the class to add a debug mode:
class DebugZalgo extends Zalgo {
public function apply($text, bool $debug = false) {
return $debug ? $text : parent::apply($text);
}
}
Character Limits: Test with edge cases:
$zalgo->apply(""); // Empty string
$zalgo->apply(str_repeat("a", 1000)); // Long string
$zalgo->apply("😊"); // Emoji (may not work as expected)
Custom Diacritics:
Override the getDiacritics() method to add/remove symbols:
class CustomZalgo extends Zalgo {
protected function getDiacritics() {
$diacritics = parent::getDiacritics();
$diacritics[] = "̴"; // Add your own
return $diacritics;
}
}
Probability Control: Adjust the likelihood of applying diacritics by modifying the internal logic (e.g., reduce the randomness factor):
class LightZalgo extends Zalgo {
protected function applyDiacritic($char) {
if (mt_rand(0, 2) === 0) { // 33% chance (default is ~50%)
return $char . $this->getRandomDiacritic();
}
return $char;
}
}
Laravel Service Provider: Bind the package to the container for easy dependency injection:
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton(Zalgo\Zalgo::class, function() {
return new Zalgo\Zalgo();
});
}
Zalgo class.How can I help you explore Laravel packages today?