PHP code example of waad / laravel-profanity-filter

1. Go to this page and download the library: Download waad/laravel-profanity-filter 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/ */

    

waad / laravel-profanity-filter example snippets


use Waad\ProfanityFilter\Facades\ProfanityFilter;

$text = "This is a test string with some profanity like fuck and shit.";
$hasProfanity = ProfanityFilter::hasProfanity($text);

echo $hasProfanity; // true

use Waad\ProfanityFilter\Facades\ProfanityFilter;

$text = "This is a test string with some profanity like fuck and shit.";
$filteredText = ProfanityFilter::filter($text);

echo $filteredText; // This is a test string with some profanity like **** and ****.

use Waad\ProfanityFilter\Facades\ProfanityFilter;

$text = "This is a test string with some profanity like fuck and shit.";
$profanityWords = ProfanityFilter::getProfanityWords($text);

print_r($profanityWords);
// Output: Array ( [0] => fuck [1] => shit )

use Waad\ProfanityFilter\Facades\ProfanityFilter;

ProfanityFilter::setLanguage('en'); // default is null (auto detect language)

$text = "This is a test string with some profanity like fuck and shit.";
$filteredText = ProfanityFilter::filter($text);

echo $filteredText; // This is a test string with some profanity like **** and ****.

use Waad\ProfanityFilter\Facades\ProfanityFilter;

ProfanityFilter::setCaseSensitive(true); // default is false

$text = "This is a test string with some profanity like fuck and Fuck.";
$filteredText = ProfanityFilter::filter($text);

echo $filteredText; // This is a test string with some profanity like **** and Fuck.

use Waad\ProfanityFilter\Facades\ProfanityFilter;

ProfanityFilter::setDetectLeetSpeak(true); // default is true

$text = "This is a test string with some profanity like f@ck and sh!t.";
$filteredText = ProfanityFilter::filter($text);

echo $filteredText; // This is a test string with some profanity like **** and ****.

use Waad\ProfanityFilter\Facades\ProfanityFilter;

config(['profanity-filter.custom_words.en' => ['custom']]);
ProfanityFilter::setConfig(config('profanity-filter'));

$text = "This is a test string with some profanity like fuck and shit.";
$filteredText = ProfanityFilter::filter($text);

echo $filteredText; // This is a test string with some profanity like **** and ****.

use Waad\ProfanityFilter\Facades\ProfanityFilter;

// Import words from a TXT file
ProfanityFilter::importWordsFromFile(storage_path('app/profanity-words.txt'), 'en');

// Import words from a JSON file
ProfanityFilter::importWordsFromFile(storage_path('app/profanity-words.json'), 'en');

$text = "This is foo and bar and alpha.";
$filteredText = ProfanityFilter::filter($text);

echo $filteredText; // This is *** and *** and *****.


use Waad\ProfanityFilter\Facades\ProfanityFilter;

// Add a single word
ProfanityFilter::addWords('badword', 'en');

// Add multiple words at once
ProfanityFilter::addWords(['testword1', 'testword2'], 'en');
ProfanityFilter::addWords(collect(['testword3', 'testword4']), 'en');

$text = "This is badword and testword1 and testword3 and testword4.";
$filteredText = ProfanityFilter::filter($text);

echo $filteredText; // This is ******* and ********* and ********* and *********.


use Waad\ProfanityFilter\Facades\ProfanityFilter;

// Remove a single word
ProfanityFilter::removeWords('damn', 'en');

// Remove multiple words at once
ProfanityFilter::removeWords(['shit', 'hell'], 'en');

$text = "This is damn and shit.";
$hasProfanity = ProfanityFilter::hasProfanity($text);

echo $hasProfanity; // false


use Waad\ProfanityFilter\Facades\ProfanityFilter;

// Clear all words for English
ProfanityFilter::clearWords('en');

$text = "This is damn and shit.";
$hasProfanity = ProfanityFilter::hasProfanity($text);

echo $hasProfanity; // false


use Waad\ProfanityFilter\Facades\ProfanityFilter;

$words = ProfanityFilter::getWords('en');

print_r($words);
// Output: Array ( [0] => damn [1] => shit [2] => ... )


use Waad\ProfanityFilter\Facades\ProfanityFilter;

$text = "This is a test string with some profanity like f@ck and sh!t.";
ProfanityFilter::setLanguage('en')
    ->setDetectLeetSpeak(true)
    ->setCaseSensitive(false)
    ->addWords('customword', 'en');

ProfanityFilter::hasProfanity($text); // true
ProfanityFilter::filter($text); // This is a test string with some profanity like *@** and **!*.

// Chaining word management methods
ProfanityFilter::addWords(['word1', 'word2'], 'en')
    ->removeWords('word1', 'en')
    ->clearWords('fr');

$words = ProfanityFilter::getWords('en');
bash
php artisan vendor:publish --tag="profanity-filter"
bash
php artisan vendor:publish --tag="profanity-words"