<?php
$badwords = array('dog', 'dala', 'bad3', 'ass');
$text = 'This is a dog. . Grass. is good but ass is bad.';
print_r( filterBadwords($text,$badwords));
function filterBadwords($text, array $badwords, $replaceChar = '*') {
// if(preg_match())
$repu = preg_replace_callback(array_map(function($w) { return '/\b' . preg_quote($w, '/') . '\b/i'; }, $badwords),
function($match) use ($replaceChar) {
echo "in";
return str_repeat($replaceChar, strlen($match[0])); },
$text
);
return array('error' =>'Match/No Match', 'text' => $repu );
}
preferences:
49.38 ms | 402 KiB | 5 Q