I'm attempting to create a bad word filter in PHP that will search a text, match against an array of known bad words, then replace each character (except the first letter) in the bad word with an asterisk.
Example:
fook
would become f***
shoot
would become s****
The only part I don't know is how to keep the first letter in the string, and how to replace the remaining letters with something else while keeping the same string length.
My code is unsuitable because it always replaces the whole word with exactly 3 asterisks.
$string = preg_replace("/\b(". $word .")\b/i", "***", $string);
$string = 'fook would become';
$word = 'fook';
$string = preg_replace("~\b". preg_quote($word, '~') ."\b~i", $word[0] . str_repeat('*', strlen($word) - 1), $string);
var_dump($string);