Laravel Docs

Laravel Profanify: A Package to Filter Profanity in Your Applications

Sophia Ramirez

Sophia Ramirez

2 weeks ago 4 Minutes Read
Packages Laravel
Profanify

In today's digital age, ensuring that user-generated content is appropriate and free from offensive language is a critical concern for developers. Whether you're building a social media platform, a comment section, or a chat application, filtering out profanity is essential to maintain a positive and respectful environment. Enter Profanify, a powerful Laravel package designed to help you easily detect and filter profane language in your applications.

In this blog post, we'll explore what Profanify is, how it works, and how you can integrate it into your Laravel projects.

What is Profanify?

Profanify is a Laravel package created by Jon Purvis that provides a simple yet effective way to detect and filter profanity in text. It uses a comprehensive list of profane words and phrases to identify inappropriate content, making it easier for developers to enforce community guidelines and maintain a clean user experience.

The package is lightweight, easy to use, and highly customizable, allowing you to tailor it to your specific needs. Whether you want to replace profane words with asterisks, completely block submissions containing profanity, or simply log instances of inappropriate language, Profanify has you covered.

Key Features of Profanify

  1. Profanity Detection: Profanify comes with a built-in list of profane words and phrases, making it easy to detect inappropriate language in user input.
  2. Customizable Word List: You can extend or modify the default list of profane words to suit your application's requirements.
  3. Text Filtering: The package allows you to replace profane words with placeholders (e.g., asterisks) or take other actions, such as rejecting the input entirely.
  4. Case-Insensitive Matching: Profanify detects profanity regardless of the case used, ensuring that users can't bypass the filter by altering capitalization.
  5. Easy Integration: The package is designed to work seamlessly with Laravel, making it simple to integrate into your existing projects.

How to Install Profanify

Getting started with Profanify is straightforward. Follow these steps to install and configure the package in your Laravel application:

Step 1: Install the Package via Composer

Run the following command in your terminal to install Profanify:

composer require jonpurvis/profanify

Profanify comes with a configuration file that allows you to customize its behavior. Publish the configuration file using the following command:

php artisan vendor:publish --provider="JonPurvis\Profanify\ProfanifyServiceProvider"

This will create a profanify.php file in your config directory.

Step 3: Customize the Word List (Optional)

If you want to add or remove words from the default profanity list, you can do so by editing the profanify.php configuration file. The words array contains the list of profane terms that Profanify will detect.

return [
    'words' => [
        'badword1',
        'badword2',
        // Add or remove words as needed
    ],
];

Using Profanify in Your Application

Once installed, you can start using Profanify to filter text in your application. Here are a few examples of how you can use the package:

Example 1: Filtering Profanity in a String

You can use the Profanify facade to check and filter a string for profanity:

use JonPurvis\Profanify\Facades\Profanify;
$text = "This is a badword1 example.";
$filteredText = Profanify::filter($text);
echo $filteredText; // Output: "This is a ******** example."

Example 2: Checking for Profanity

If you simply want to check whether a string contains profanity, you can use the hasProfanity method:

use JonPurvis\Profanify\Facades\Profanify;
$text = "This is a badword1 example.";
if (Profanify::hasProfanity($text)) {
    echo "Profanity detected!";
} else {
    echo "No profanity found.";
}

Example 3: Custom Replacement Text

You can customize the replacement text used for profane words by passing a second argument to the filter method:

use JonPurvis\Profanify\Facades\Profanify;
$text = "This is a badword1 example.";
$filteredText = Profanify::filter($text, '[CENSORED]');
echo $filteredText; // Output: "This is a [CENSORED] example."

Why Use Profanify?

Profanify is an excellent tool for developers who want to maintain a professional and respectful environment in their applications. Here are a few reasons why you should consider using it:

  • Saves Time: Instead of building a profanity filter from scratch, you can leverage Profanify's ready-to-use solution.
  • Customizable: The package is highly flexible, allowing you to tailor it to your specific needs.
  • Community-Driven: Profanify is open-source, meaning it benefits from contributions and improvements by the developer community.

Conclusion

Profanify is a valuable addition to any Laravel developer's toolkit, providing an easy and effective way to filter profanity in user-generated content. By integrating this package into your application, you can ensure a cleaner, more respectful environment for your users.

To learn more about Profanify and explore its source code, check out the GitHub repository. If you find the package useful, don't forget to give it a star and contribute to its development!

Have you used Profanify in your projects? Share your experiences and tips in the comments below!

Sophia Ramirez

Sophia Ramirez

Software Engineer

Sophia specializes in creating and maintaining Laravel packages. She has developed numerous popular open-source packages and shares her expertise in extending Laravel functionality through clean, reusable code.

Discussion (0)

Log in to comment!

No comments yet!

Releted Posts

Newsletter

Subscribe to my newsletter to get updated posts

Don't worry, I don't spam