PHP code example of yasser-elgammal / pure-text

1. Go to this page and download the library: Download yasser-elgammal/pure-text library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yasser-elgammal / pure-text example snippets


use YasserElgammal\PureText\Traits\PureTextFilterable;

class Post extends Model
{
    use PureTextFilterable;

    protected $filterable = ['title', 'content'];
}

$post = new Post();
$post->title = "This is a badword example";
$post->save();

echo $post->title; // "This is a *** example"

use YasserElgammal\PureText\Facades\PureText;

// Filter text
$clean = PureText::filter('Some bad text');

// Check if text contains bad words
if (PureText::containsBadWords($text)) {
    // Handle inappropriate content
}

// Get all bad words found
$words = PureText::getBadWords($text); // ['bad', 'ugly']

// Whitelist specific words on-the-fly
$clean = PureText::withWhitelist(['cocktail'])
    ->filter('Order a cocktail, not a badword');

// Output: "Order a cocktail, not a ***"

// In config/badwords.php:
'whitelist' => ['cocktail', 'document', 'classic'],

// Or dynamically in code:
PureText::withWhitelist(['badlands'])
    ->containsBadWords('Exploring the badlands'); // false

// With 'سيء' and 'احمق' in your bad words list:

PureText::filter('هذا سَيِّءٌ جداً');
// Output: "هذا *** جداً" (tashkeel stripped)

PureText::filter('هذا ســــيء جداً');
// Output: "هذا *** جداً" (tatweel stripped)

PureText::filter('هذا أحمق');
// Output: "هذا ***" (matches 'احمق' without hamza)

// With 'bad', 'ass', and 'shit' in your bad words list:

PureText::filter('This is b4d');
// Output: "This is ***"

PureText::filter('You are an @ss');
// Output: "You are an ***"

PureText::filter('Holy sh!t');
// Output: "Holy ***"

$request->validate([
    'title'   => '

    // Allow specific words for this field:
    'recipe'  => '

// Filter all string fields
Route::post('/comment', [CommentController::class, 'store'])
    ->middleware('pure-text');

// Filter specific fields only
Route::post('/comment', [CommentController::class, 'store'])
    ->middleware('pure-text:title,content');

// Filter text
$clean = pure_text('Some bad text');

// Check for bad words
if (has_bad_words($text)) {
    // Handle inappropriate content
}

return [
    // List of bad words to filter
    'words' => ['badword1', 'badword2'],

    // Whitelist words/phrases that should never be flagged
    'whitelist' => ['cocktail', 'classic', 'document'],

    // Normalize Arabic diacritics (tashkeel), tatweel, and letter forms
    'normalize_arabic' => true,

    // Detect leetspeak (b4d, @ss, sh!t, p0rn)
    'detect_leetspeak' => true,

    // Replacement text (used with 'fixed' strategy)
    'replacement' => '***',

    // Strategy: 'fixed', 'character', or 'length_match'
    'strategy' => 'fixed',

    // Character used with 'character' strategy
    'character' => '*',
];

use YasserElgammal\PureText\Contracts\TextFilterInterface;

$this->app->singleton(TextFilterInterface::class, function () {
    return new MyCustomFilterService();
});

use YasserElgammal\PureText\Events\TextFiltered;

class TextFilteredListener
{
    public function handle(TextFiltered $event): void
    {
        logger('Filtered text on model', [
            'original'  => $event->originalText,
            'filtered'  => $event->filteredText,
            'model'     => $event->model?->getTable(),
            'attribute' => $event->attribute,
        ]);
    }
}
bash
php artisan vendor:publish --tag=pure-text-config