<?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 = '*') {
//$match_found = 0;
$repu = preg_replace_callback(array_map(function($w) { return '/\b' . preg_quote($w, '/') . '\b/i'; }, $badwords),
function($match) use ($replaceChar) {
$match_found = 1;
return str_repeat($replaceChar, strlen($match[0])); },
$text
);
if(str_icmp($repu,$text) === 0)
echo "ok";
else
echo "not ok";
return array('error' =>$match_found, 'text' => $repu );
}
preferences:
41.72 ms | 402 KiB | 5 Q