DEV Community

Aleson França
Aleson França

Posted on

A Deep Dive into Laravel: Str Helper Methods

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@example.com', '@'); // "example.com"
Enter fullscreen mode Exit fullscreen mode

Str::before($str, $search)

Returns everything before the first occurrence of $search.

echo Str::before('email@example.com', '@'); // "email"
Enter fullscreen mode Exit fullscreen mode

Str::contains($str, $needles)

Check if the string contains a specific value.

if (Str::contains('Laravel Framework', 'Laravel')) {
    echo "Contains Laravel";
}
Enter fullscreen mode Exit fullscreen mode

Str::endsWith($str, $needles)

Checks if the string ends with a specific value.

if (Str::endsWith('filename.jpg', '.jpg')) {
    echo "It is an image";
}
Enter fullscreen mode Exit fullscreen mode

Str::startsWith($str, $needles)

Checks if the string starts with a specific value.

if (Str::startsWith('Laravel Framework', 'Laravel')) {
    echo "Starts with Laravel";
}
Enter fullscreen mode Exit fullscreen mode

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..."
Enter fullscreen mode Exit fullscreen mode

Str::title($str)

Converts each word in the string to title case.

echo Str::title('laravel framework'); // "Laravel Framework"
Enter fullscreen mode Exit fullscreen mode

Str::slug($str, $separator = '-')

Generates a URL-friendly slug.

echo Str::slug('Laravel 10 is amazing!'); // "laravel-10-is-amazing"
Enter fullscreen mode Exit fullscreen mode

Str::random($length)

Generates a random string of a given length.

echo Str::random(10); // Example: "aB3kLmPqXz"
Enter fullscreen mode Exit fullscreen mode

Str::replace($search, $replace, $subject)

Replaces all occurrences of $search with $replace.

echo Str::replace('world', 'Laravel', 'Hello world'); // "Hello Laravel"
Enter fullscreen mode Exit fullscreen mode

Str::replaceFirst($search, $replace, $subject)

Replaces only the first occurrence of $search with $replace.

echo Str::replaceFirst('world', 'Laravel', 'world world'); // "Laravel world"
Enter fullscreen mode Exit fullscreen mode

Str::take($str, $length)

Returns the first $length characters of the string.

echo Str::take('Laravel Framework', 7); // "Laravel"
Enter fullscreen mode Exit fullscreen mode

Str::wrap($str, $before, $after = null)

Wraps the string with the specified values.

echo Str::wrap('Laravel', '<b>', '</b>'); // "<b>Laravel</b>"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)