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 it
}
// Get all bad words found
$words = PureText::getBadWords($text); // ['bad', 'ugly']
$request->validate([
'title' => '
]);
// 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 it
}
return [
// List of bad words to filter
'words' => ['badword1', 'badword2'],
// 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,
]);
}
}