How To Use Laravel’s Str::repeat For String Manipulation And Directory Formatting
Published on February 2, 2025 by Dinesh Uprety

How to Repeat Strings and Format Directory Listings in Laravel
When working with strings in Laravel, you may often need to repeat a character or string multiple times. Laravel provides the Str::repeat helper method, which allows you to easily repeat a string a specified number of times. Additionally, this method can be used creatively for formatting directory structures, as we’ll explore in this article.
Understanding the repeat Method
The repeat method is available in Laravel 8, 9 and 11.x and has the following signature:
<?php /** * Versions: Laravel 8, 9 and 11.x * * @return string */public static function repeat( string $string, int $times);
This method takes two parameters:
-
$string
– The string you want to repeat. -
$times
– The number of times the string should be repeated.
Basic Example: Repeating a String
A simple use case is generating repeated characters, such as a line of asterisks.
<?php use Illuminate\Support\Str; echo Str::repeat('*', 10); // Output: **********
Alternatively, you can use fluent string instances:
<?php echo (string) str('*')->repeat(10); // Output: **********
Practical Use Case: Formatting a Directory Structure
The Str::repeat
method is especially useful when formatting text output. Let’s use it to create a function that *
pretty-prints a directory listing recursively*.
<?php use Illuminate\Support\Str; function printDirectory($directory, $depth = 1){ $output = ''; $ignoreFiles = ['.', '..']; if ($depth == 1) { $output = '/' . basename($directory); } else { $output = Str::repeat(' ', $depth) . '/' . basename($directory); } $output .= collect(scandir($directory)) ->filter(fn($file) => !in_array($file, $ignoreFiles)) ->map(function ($item) use ($directory, $depth) { $fullPath = $directory . '/' . $item; if (is_dir($fullPath)) { return "\n" . printDirectory($fullPath, $depth + 1); } else { return "\n" . Str::repeat(' ', $depth) . ' - ' . $item; } }) ->join(''); return $output;}
How It Works
- The function scans a directory and retrieves all files and subdirectories.
- It uses
Str::repeat(' ', $depth)
to indent nested items based on their depth. - When a directory is encountered, the function calls itself recursively, increasing the depth each time.
Example Output
If we call this function on Laravel’s resources directory:
<?php echo printDirectory(resource_path());
We get the following output:
/resources /css - app.css /js - app.js - bootstrap.js /views - welcome.blade.php
And for the public
directory:
/public /css - app.css /js - app.js - bootstrap.js
This neatly formatted output provides a clear overview of the directory structure.
Conclusion
The Str::repeat
helper method in Laravel is a simple yet powerful tool for repeating strings. Whether you’re
generating
repeated characters or formatting hierarchical data like directory structures, this method provides a clean and
efficient solution.