Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here
Premium Members Only
Join to unlock this tutorial and all of our courses.
Tutorial Premium Tutorial

Laravel String Helpers: 16 Open-Source Examples

November 14, 2023
7 min read

Laravel has a lot of valuable helper functions. This post will show 16 examples of string helpers and how they are used in real projects.


Notice: String Syntax Changes

Before we begin, a little notice: you may find different variants of the same String functions, so you would understand the differences, here's what were added in Laravel lately.

  • In Laravel 7, they added Fluent String Operations. You may create a fluent Illuminate\Support\Stringable object using the Str::of() method. All fluent string methods can be found in the official documentation.
  • In Laravel 9, they added a new str() helper. The str() function is shorter and equivalent to the Str::of() method, which returns the Illuminate\Support\Stringable.

1. Str::slug()

The easiest way to create a slug from a given string without using any external packages is the Str::slug() helper.

An example is from an open-source platform BookStackApp.

use Illuminate\Support\Str;
 
class ImageStorage
{
// ...
 
public function cleanImageFileName(string $name): string
{
$name = str_replace(' ', '-', $name);
$nameParts = explode('.', $name);
$extension = array_pop($nameParts);
$name = implode('-', $nameParts);
$name = Str::slug($name);
 
if (strlen($name) === 0) {
$name = Str::random(10);
}
 
return $name . '.' . $extension;
}
 
// ...
}

Link to the example in GitHub repository.


2. Str::title()

Create a title from a string where the first letter of every word starts with a capital letter using Str::title() helper.

An example is from an open-source platform BookStackApp.

// ...
<h1 class="list-heading">{{ Str::title(trans('auth.log_in')) }}</h1>
// ...

Link to the example in GitHub repository.


3. Str::startsWith()

If you need to check whether a string starts with a given value, the Str::startsWith() helper is just for that.

An example is from an open-source project Pterodactyl.

use Illuminate\Support\Str;
 
class ActivityLogged extends Event
{
// ...
 
public function isServerEvent(): bool
{
return Str::startsWith($this->model->event, 'server:');
}
 
// ...
}

Link to the example in GitHub repository.


4. Str::endsWith()

Opposite to the Str::startsWith(), the Str::endsWith() helper checks if a given string end with a provided value.

An example is from an open-source project creater.

use Illuminate\Support\Str;
 
class PathToZip implements Rule
{
// ...
 
public function passes($attribute, $value)
{
return Str::endsWith($value, '.zip');
}
 
// ...
}

Link to the example in GitHub repository.


5. Str::before() and basename()

The Str::before() helper returns everything from a string before a given value.

An example is from...

Premium Members Only

This advanced tutorial is available exclusively to Laravel Daily Premium members.

Premium membership includes:

Access to all premium tutorials
Video and Text Courses
Private Discord Channel

Comments & Discussion

H
Husniddin ✓ Link copied!

I didn't know about Str::kebab

First i saw that was a joke )

CW
Caio Wilson ✓ Link copied!

I don't get why the camel chained with kebab, wouldn't kebab only suffice?

CW
Caio Wilson ✓ Link copied!

BTW lcfirst and ucsplit are awesome too.

CW
Caio Wilson ✓ Link copied!

Famously known as just read the docs LUL

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.