Support the ongoing development of Laravel.io →
Article Hero Image

How to Filter Profanity in Laravel with Squeaky

4 Mar, 2025 5 min read

Introduction

When you accept user input in your web applications, you may want to validate it to ensure it doesn't contain profanity. I've needed to implement this functionality in several past projects, and I'm sure many of you have, too.

One way to filter profanity in your Laravel apps is to use Squeaky, a new package by Jon Purvis.

Squeaky is a Laravel package containing a validation rule which ensures values don't contain profanity.

I've written about Jon's other PHP packages before, and I'm always impressed with the quality of his work. So I thought I'd write a quick article about Squeaky since it's a package I think many of you will find useful.

Articles about Jon's other PHP packages:

Squeaky piggybacks off Jon's other PHP package, Profanify, which is a Pest plugin for preventing profanity in your PHP codebase. For example, it checks you haven't used any profanity in your class names, method names, or comments. So you might want to check that out if you're interested in this topic.

How to Filter Profanity in Laravel

Let's look at how to use Squeaky to filter profanity in Laravel.

Installation

To get started with using Squeaky, you'll first need to install it. You can do this using Composer by running the following command in your project's root:

composer require jonpurvis/squeaky

It should now be installed and ready to use in your Laravel application.

Using the Rule

To use the rule, you can create a new instance of it and pass it to your validator.

For example, let's imagine we have a blogging platform, and we allow users to leave comments on blog posts. We'll want to ensure that the comments don't contain any profanity.

So, we'll assume we have a form request class which handles the validation of the comment. The form request may look something like this:

declare(strict_types=1);

namespace App\Http\Requests;

use JonPurvis\Squeaky\Rules\Clean;
use Illuminate\Foundation\Http\FormRequest;

final class StoreCommentRequest extends FormRequest
{
    // ...

    public function rules(): array
    {
        return [
            'comment' => ['required', 'string', new Clean()],
        ];
    }
}

As we can see in the code example, we've created an instance of the JonPurvis\Squeaky\Rules\Clean rule and added it as a validation rule for the comment field. This will ensure that the comment doesn't contain any profanity.

If no profanity is found, the validation will pass. If profanity is found, the validation will fail with a message like this:

The comment field is not clean

Specifying the Locales

By default, Squeaky will use the locale of your Laravel application. In a lot of cases, this will be sufficient.

But there may be times when you want to specify the locales you want to check against. For example, if your application supports multiple languages, you might want to check for profanity in each of those languages.

At the time of writing this article, Squeaky supports the following languages:

  • English
  • Italian
  • Arabic
  • Portuguese
  • Dutch

To specify the locales to validate against, you can pass them as an array to the JonPurvis\Squeaky\Rules\Clean rule:

declare(strict_types=1);

namespace App\Http\Requests;

use JonPurvis\Squeaky\Enums\Locale;
use JonPurvis\Squeaky\Rules\Clean;
use Illuminate\Foundation\Http\FormRequest;

final class StoreCommentRequest extends FormRequest
{
    // ...

    public function rules(): array
    {
        return [
            'comment' => ['required', 'string', new Clean([Locale::English])],
        ];
    }
}

In the example above, we're only checking for profanity in the English language.

Alternatively, you can pass multiple locales if you want to check against more than one language:

declare(strict_types=1);

namespace App\Http\Requests;

use JonPurvis\Squeaky\Enums\Locale;
use JonPurvis\Squeaky\Rules\Clean;
use Illuminate\Foundation\Http\FormRequest;

final class StoreCommentRequest extends FormRequest
{
    // ...

    public function rules(): array
    {
        return [
            'comment' => [
                'required',
                'string',
                new Clean([Locale::English, Locale::Italian]),
            ],
        ];
    }
}

In this example, we're checking for profanity in both English and Italian. If profanity is found in either language, the validation will fail.

Use Squeaky as Part of a Wider Strategy

Squeaky is a fantastic package, and I'll be using it in my projects moving forward.

But it's important to remember that detecting profanity is a complex topic. It's not always as simple as filtering out a list of words. People can be cunning and creative with their language and find ways to bypass filters. I'm sure you've seen some creative ways people bypass filters on games and social media platforms.

So, although packages like Squeaky can be incredibly useful and catch a lot of profanity, it should be used as part of a wider strategy to moderate user input. For example, you might want to consider manual moderation or provide a way for users to report inappropriate content. But this is all dependent on the context of your application.

If you have any ideas or strategies to make Squeaky's filtering more robust and powerful, I'm sure Jon would love to hear them. So make sure to check out the package on GitHub: https://github.com/JonPurvis/squeaky.

Conclusion

Hopefully, this article has given you a quick introduction to Squeaky. If you're interested in filtering profanity in your Laravel applications, it's definitely worth checking out.

If you enjoyed reading this post, I'd love to hear about it. Likewise, if you have any feedback to improve the future ones, I'd also love to hear that too.

You might also be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.

Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.

If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter.

Keep on building awesome stuff! 🚀

Last updated 4 days ago.

driesvints liked this article

1
Like this article? Let the author know and give them a clap!
ash-jc-allen (Ash Allen) I'm a freelance Laravel web developer from Preston, UK. I maintain the Ash Allen Design blog and get to work on loads of cool and exciting projects 🚀

Other articles you might like

Article Hero Image March 4th 2025

Craft Emails with React and Tailwind using Inertia Mailable

How to easily build dynamic email templates while keeping your React and Tailwind tools using the I...

Read article
Article Hero Image February 17th 2025

Preventing Destructive Commands from Running in Laravel

Introduction I recently stumbled upon a cool Laravel feature I hadn't come across before: the abilit...

Read article
Article Hero Image January 14th 2025

Serve a Laravel project on Web, Desktop and Mobile with Tauri

How to display a Laravel project simultaneously on the web, your operating system, and your mobile d...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.