Home > Backend Development > PHP Tutorial > Pure Text Laravel Package

Pure Text Laravel Package

Patricia Arquette
Release: 2024-11-12 12:05:02
Original
641 people have browsed it

Pure Text Laravel Package

PureText

PureText is a Laravel package for filtering and replacing inappropriate or unwanted words within model attributes automatically. Designed to be customizable and efficient, PureText allows developers to specify filterable attributes for each model.


Features

  • Automatic Filtering: Automatically filters designated model attributes upon saving.
  • Customizable Words List: Easily modify the list of inappropriate words and replacements from the config file.
  • Language Support: Works with multiple languages, including Arabic and other non-Latin character sets.
  • Trait Integration: Apply the Filterable trait to models, specifying which attributes should be filtered.
  • Service Provider Configuration: Provides easy configuration via a service provider and includes a singleton service for optimal performance.

Installation

  1. Install the package via Composer:

    composer require yasser-elgammal/pure-text
    
    Copy after login
  2. Publish the configuration file:

    php artisan vendor:publish --provider="YasserElgammal\PureText\PureTextServiceProvider"
    
    Copy after login
  3. Configure your list of words to filter in the config/badwords.php file.

Usage

  1. Add the Trait to Your Model

Use the PureTextFilterable trait in any model where you need to filter specific attributes.

```php
use YasserElgammal\PureText\Traits\PureTextFilterable;

class Post extends Model
{
    use PureTextFilterable;

    protected $filterable = ['title', 'content'];
}
```
Copy after login
  1. Configuring Filterable Attributes

Define protected $filterable on the model with an array of attribute names you want to filter.

Configuration

The configuration file badwords.php allows you to define:

  • words: An array of bad words that should be filtered.
  • replacement: The replacement text for filtered words, defaulting to ***.

Example

Here's a basic example of usage in a controller:

$post = new Post();
$post->title = "This is a badword example";
$post->content = "Some more text with badword";
$post->save();

echo $post->title; // Outputs: This is a ***
Copy after login

Github Link

I would greatly appreciate your support by giving it a star.

Thanks ?

The above is the detailed content of Pure Text Laravel Package. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template