最初に単語のファイル全体を配列に処理してから、ファイル内の単語のいずれかに渡された文字列をチェックするこのコードがあります。一致する場合は、* に置き換えます。
ファイルは次のようなものです:
wordalex
wordjordan
wordjohn
....
残念ながら、私が渡した文は、私が期待する方法でフィルタリングされていません. 実際には何も起こりません。与えられたコードを見て、助けてください。
$comment = "the wordalex if this doesn't get caught!";
$filterWords = file('badwords.txt', FILE_IGNORE_NEW_LINES);
//print_r($filterWords);
$textToPrint = filterwords($comment,$filterWords );
echo $textToPrint;
function filterwords($text, $filterArray){
$filterCount = sizeof($filterWords);
for($i=0; $i<$filterCount; $i++){
$text = preg_replace('/\b'.$filterWords[$i].'\b/ie',"str_repeat('*',strlen('$0'))",$text);
}
return $text;
}
そのため、実際には元の文に吟遊詩人の言葉がありますが、投稿の目的で削除されています。
ありがとうございました