In this article, we will discuss Most Usable Laravel String Helpers. As we know, Laravel provides rich features to make development more flexible. Sometimes we need “String Helpers” to manipulate string value. In core project, we need to create our custom string helpers. Laravel contains predefined string helpers, so no need to create string helpers for basic requirements.
Today, we discuss some of most usable Laravel String Helpers.
str_slug()
The str_slug function is used to generates a URL friendly “slug” from the given string. For e.g:
$slug = str_slug('Most Useable String Helpers in Laravel', '-'); // most-useable-string-helpers-in-laravel
str_random()
The str_random function is used to generates a random string of the specified length. This function uses PHP’s random_bytes function. For e.g:
$random = str_random(50); // Fh0WG6g2I6bBfIcViJtXoEp9g8g9bFXSZhfkcx4yrxDvmS3iwU
title_case()
The title_case function is used to converts the given string to Title Case. For e.g:
$converted = title_case('most useable string helpers in laravel'); // Most Useable String Helpers In Laravel
_()
The __() function is used to translates the given translation string or translation key using your localization files. As similar to trans function. For e.g:
echo __('Welcome to our application'); echo __('messages.welcome');
The __() function will return the given value If the translation string or key does not exist.
trans()
The trans function is used to translates the given translation key using your localization files. For e.g:
echo trans('messages.welcome');
The trans function will return the given key If the translation key does not exist.
In both __() and trans() function would return messages.welcome if the translation key does not exist.
Conclusion
In this article, we are discussing the Most Useable Laravel String Helpers. You can check the full list of string helper functions here. You can found more helpful tutorials here and please feel free to add any comment if any query.
If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee