Laravel provides the Str
helper, which offers many useful functions for string manipulation. String operations are essential in Laravel applications for handling user input, formatting output, and working with data transformations efficiently.
Str::after($str, $search)
Returns everything after the first occurrence of $search
.
use Illuminate\Support\Str;
echo Str::after('[email protected]', '@'); // "example.com"
Str::before($str, $search)
Returns everything before the first occurrence of $search
.
echo Str::before('[email protected]', '@'); // "email"
Str::contains($str, $needles)
Check if the string contains a specific value.
if (Str::contains('Laravel Framework', 'Laravel')) {
echo "Contains Laravel";
}
Str::endsWith($str, $needles)
Checks if the string ends with a specific value.
if (Str::endsWith('filename.jpg', '.jpg')) {
echo "It is an image";
}
Str::startsWith($str, $needles)
Checks if the string starts with a specific value.
if (Str::startsWith('Laravel Framework', 'Laravel')) {
echo "Starts with Laravel";
}
Str::limit($str, $limit, $end = '...')
Truncates the string to the specified limit.
echo Str::limit('This is a very long text', 10); // "This is a..."
Str::title($str)
Converts each word in the string to title case.
echo Str::title('laravel framework'); // "Laravel Framework"
Str::slug($str, $separator = '-')
Generates a URL-friendly slug.
echo Str::slug('Laravel 10 is amazing!'); // "laravel-10-is-amazing"
Str::random($length)
Generates a random string of a given length.
echo Str::random(10); // Example: "aB3kLmPqXz"
Str::replace($search, $replace, $subject)
Replaces all occurrences of $search
with $replace
.
echo Str::replace('world', 'Laravel', 'Hello world'); // "Hello Laravel"
Str::replaceFirst($search, $replace, $subject)
Replaces only the first occurrence of $search
with $replace
.
echo Str::replaceFirst('world', 'Laravel', 'world world'); // "Laravel world"
Str::take($str, $length)
Returns the first $length
characters of the string.
echo Str::take('Laravel Framework', 7); // "Laravel"
Str::wrap($str, $before, $after = null)
Wraps the string with the specified values.
echo Str::wrap('Laravel', '', ''); // "Laravel"
Str::trans($key, $replace = [], $locale = null)
Returns a translated string using a localization key.
echo Str::trans('messages.welcome'); // Assuming a translation is set up
Str::transChoice($key, $number, $replace = [], $locale = null)
Returns a translated string with pluralization support.
echo Str::transChoice('messages.apple', 1); // "1 apple"
echo Str::transChoice('messages.apple', 5); // "5 apples"
Conclusion
The Str
helper in Laravel is a powerful tool for string manipulation, offering practical and efficient solutions for everyday development needs. Whether you need to format, replace, truncate, or generate slugs and random strings, Str
helps make your code cleaner and more readable.
Additionally, its integration with localization features (trans
and transChoice
) makes internationalization easier. If you want to write concise and expressive code, mastering Str
is essential!
Do you already use these methods in your projects? Which one is your favorite? Share your thoughts in the comments and let's discuss! 🚀
For more details, check out the official laravel doc: Laravel Str Helper