Laravel String Helpers

laravel-string-helpers

In addition to all of the common functionality we’ve come to know and love in Laravel such as routing, mail, file storage, middleware, authentication, and more, Laravel also provides some pretty cool helper functions to make common tasks easier. In this episode we’ll take a look at some of the string helpers that are included with Laravel and test them out to see how they work. Let’s check it out now.


camel_case()

First we’ll check out the camel_case() function that accepts a string, and then converts it to camel case. For example:

This is pretty cool, let’s see how it works. In the helpers.php file we find the code that references this function. Here it is:

We can see that it is calling on a static method of camel, so let’s go see how that works. In the Str class we find it here:

Interesting. First, it looks like the value is converted to studly case, then that result is run through the lcfirst function making the first character lowercase. This studly function is part of this same class so let’s have a look at how that works.

So we can see that first off, the str_replace function is used to look for any hyphen or underscore characters in the string, and replace them with a blank space. Once this is complete, the ucwords function is applied creating a string like so:

This result is once again put through the str_replace function and any blank spaces are removed from the string.

Finally, the lcfirst function is applied to this result in the public static function camel, resulting in our camel case string, thisIsAString.


class_basename()

Next up is the class_basename() function which gives you the base class of a namespaced representation of the class. For example:

Let’s see how it works. Again in the helpers.php file we find the following.

The class_basename() function makes use of the built in PHP functions is_object() and get_class(), along with str_replace().


e()

Moving along and we come to the e() helper function, which makes it a whole lot easier to make use of the htmlentities function in PHP. Let’s test it out now:

Here is the function we find in the helpers.php file:


ends_with()

The ends_with() function checks a string to see if it ends with a particular value. Let’s try it out.

The ends_with() function defers to the Str::endsWith in helpers.php, which defers to endsWith in the Str.php class file. This function in turn relies on the static substr function. Here are those snippets of code.

helpers.php

Str.php

We can see that this call to substr is actually a wrapper around the mb_substr() function. This thread at stack overflow gives us a good idea of the difference between the two.


snake_case()

This function takes a string and turns it into snake case. Let’s try it on the string we initially turned into a camel case. Recall we turned this-is_a_-string into thisIsAString using the camel_case() function. Let’s now put that result through the snake_case() function.

Upon inspecting the code, we found that you can also pass a second parameter to create your own version of the snake case. You can even do a umadbro emoji like so:

This is really cool! The snake_case() is actually fairly involved in how it works making use of regular expressions to make the magic happen.


str_limit()

This function is very handy for when you want to show a block of text, such as in a card based layout, where all blocks of text are the same length so that the display is uniform. Basically, you pass this function a string of text as the first parameter and the number of characters you would like to display as the second argument. Here is an example:


starts_with()

The starts_with() function can be used to check if a string starts with a given value.


str_contains()

Finding a value in a string is such a common task in PHP and many times we make use of strpos to complete this. Think of str_contains() as a more robust version of strpos() with an easier to reason about naming convention. Let’s see it in action here:

You can also check if the string contains multiple values using an array like so:


str_finish()

The str_finish() function acts like a string concatenator of sorts by adding a value to the end of the string. Here is our test of str_finish()


str_is()

This function can be used to test for the occurrence of a pattern in a string, and you can use one or more wildcards for the pattern. Here are a few examples.


str_plural()

The str_plural() function takes a word, and returns the plural form of it.


str_singular()

In addition to being able to pluralize a word, we can convert back to the singular with the str_singular() function. Let’s try it out.


str_random()

If you need to generate a random string with a specific length, this is the function to do it. Here we are generating a random string 30 characters in length.


str_slug()

The str_slug() function is perfect to sluggify any string. For example:


studly_case()

The studly_case() function is used to apply a studly case to a string. This means each significant word begins with an uppercase letter, including the first letter of the string. Let’s see it work.


title_case()

Similar to the studly case scenario we just examined is the title_case() function which works in a similar way, but adds a space between the words. Turn any string into a title in a snap.


Laravel String Helpers Summary

In this tutorial we had a look at most of the string helper functions available for you to use in your Laravel project. These functions exist in the helpers.php file, and make use of the static methods found in the Str.php class. These types of functions that complete small and useful tasks are perfect for a static method implementation. Have a play with these helpers, maybe you’ll find a good use case for your own project.