Lädt...

🔧 How to Filter Profanity in Laravel with Squeaky


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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! 🚀

...

🔧 How to Filter Profanity in Laravel with Squeaky


📈 76.67 Punkte
🔧 Programmierung

📰 The Making Of: qa-squeaky-toys.docm


📈 34.71 Punkte
📰 IT Security Nachrichten

🪟 You’ll soon be able to filter profanity out of Teams


📈 33.11 Punkte
🪟 Windows Tipps

🎥 Upgrade Laravel 11 to Laravel 12 #laravel #thecodeholic


📈 26.55 Punkte
🎥 Video | Youtube

🎥 Using Laravel Breeze Starter Kits in Laravel 12 #laravel #thecodeholic #laravel12


📈 26.55 Punkte
🎥 Video | Youtube

🎥 Print SQL Queries in Laravel #laravel #thecodeholic #laravelcourse #php #laravel


📈 26.55 Punkte
🎥 Video | Youtube

🎥 Create First Laravel Project using Laravel Herd #laravel #laravelproject #laravelphp


📈 26.55 Punkte
🎥 Video | Youtube

🔧 [Laravel v11 x Docker] Efficiently Set Up a Laravel App Dev Environment with Laravel Sail


📈 26.55 Punkte
🔧 Programmierung

🪟 Microsoft News: Teams profanity filtering control for live captions now in test


📈 24.62 Punkte
🪟 Windows Tipps

📰 Profanity Update: Youtube lockert das verdammte Fluchverbot


📈 24.62 Punkte
📰 IT Nachrichten

📰 Gaming YouTube Is In Turmoil Thanks To New Violence and Profanity Rules


📈 24.62 Punkte
📰 IT Security Nachrichten

🕵️ CVE-2022-40769 | profanity up to 1.60 weak prng (ID 61)


📈 24.62 Punkte
🕵️ Sicherheitslücken

🕵️ Profanity up to 0.5.0 Message Carbon spoofing


📈 24.62 Punkte
🕵️ Sicherheitslücken

🐧 XMPP - Profanity and Pass howto


📈 24.62 Punkte
🐧 Linux Tipps

🕵️ Profanity: Eine Schwachstelle ermöglicht die Darstellung falscher Informationen


📈 24.62 Punkte
🕵️ Sicherheitslücken

🕵️ Profanity bis 0.5.0 Message Carbon Spoofing


📈 24.62 Punkte
🕵️ Sicherheitslücken

📰 Study Finds Link Between Profanity and Honesty


📈 24.62 Punkte
📰 IT Security Nachrichten

📰 Study Finds Link Between Profanity and Honesty


📈 24.62 Punkte
📰 IT Security Nachrichten

🔧 Building a Multi-LLM Profanity Detector in C# using StepWise


📈 24.62 Punkte
🔧 Programmierung

🔧 How I Built a Profanity Blocking JavaScript Library 🤬 ⚡


📈 24.62 Punkte
🔧 Programmierung

🪟 Teams adds profanity filtering control for live captions


📈 24.62 Punkte
🪟 Windows Tipps

🪟 Microsoft Teams users start getting profanity filtering control – Microsoft News


📈 24.62 Punkte
🪟 Windows Tipps

🔧 Laravel News: Laravel 12 Released


📈 17.7 Punkte
🔧 Programmierung

🎥 I Built Laravel Course for Beginners #laravel #laravelphp #thecodeholic


📈 17.7 Punkte
🎥 Video | Youtube

🎥 Implement Roles & Permissions in Laravel + React #laravel #thecodeholic


📈 17.7 Punkte
🎥 Video | Youtube

🔧 Streamlining Laravel Projects with Laravel Model Status


📈 17.7 Punkte
🔧 Programmierung

🎥 Laravel 11 Crash Course - Laravel in 1 hour


📈 17.7 Punkte
🎥 Video | Youtube

🎥 Create Laravel + React App with Authentication #laravel #thecodeholic #laravelcourse


📈 17.7 Punkte
🎥 Video | Youtube

🔧 Understanding Laravel Middleware: A Deep Dive into Laravel 11's New Approach


📈 17.7 Punkte
🔧 Programmierung

🎥 Custom Artisan Commands in Laravel #laravel #thecodeholic #artisan


📈 17.7 Punkte
🎥 Video | Youtube

🔧 XAMPP vs. Laragon vs. Laravel Herd: Which One Should You Use for PHP and Laravel Projects?


📈 17.7 Punkte
🔧 Programmierung

🔧 History of Laravel: How Laravel Started and Where It Stands Today


📈 17.7 Punkte
🔧 Programmierung

matomo