cebe/indent
cebe/indent is a small PHP utility to indent or outdent multi-line strings. Useful for formatting generated code, logs, and console output; it normalizes leading whitespace and helps you apply consistent indentation levels with simple helpers.
Start by installing via Composer: composer require cebe/indent. The library is tiny — just one main class, cebe\indent\Indent. The first use case is aligning multi-line strings in CLI output or generated code. For example, to indent a help block by two spaces:
use cebe\indent\Indent;
$output = Indent::indent("Line 1\nLine 2\nLine 3", 2);
// Returns: " Line 1\n Line 2\n Line 3"
For generated code blocks inside templates, you can also auto-detect and strip common leading whitespace:
$code = Indent::dedent(" echo 'foo';\n return true;");
// Returns: "echo 'foo';\nreturn true;"
Check the README or source (src/) for minimal, well-documented code — it’s just a few hundred lines.
CLI Formatting: Use indent() to nicely align help or usage messages in console commands. Combine with str_repeat(' ', $depth) for dynamic nesting levels.
Code Generation: In scaffolders or code builders, dedent() lets you write clean multi-line strings in templates without worrying about indentation in source code. Wrap your template strings in dedent() before appending to output.
Nested Structures: For hierarchical output (e.g., generating JSON/YAML-like structures), chain operations:
$block = Indent::indent(
Indent::dedent("key:\n value1\n value2"),
4
);
// " key:\n value1\n value2"
Configurable Indentation: Use indent() with a second argument of ' ' or "\t" instead of an integer to use custom prefixes (e.g., for editor-config consistency).
Integrating with Livewire/Blade: When outputting multi-line text from Blade ({{ Indent::indent(...) }}), ensure you’re not double-escaping newlines — use implode("\n", $lines) when building arrays.
\n, \r\n, etc., but does not normalize endings — preserve consistency in your input strings.dedent() removes common leading whitespace per line, so a trailing newline that’s indented separately (e.g., " foo\n \nbar") can behave unexpectedly — trim inputs or use trim() if needed.dedent() treats lines consisting of only whitespace as contributing to the minimum indentation — they’ll be reduced to nothing. For logs where blank lines must remain, sanitize before dedent().Indenter service for DI or project-specific defaults (e.g., always indent by 4 spaces).How can I help you explore Laravel packages today?