I'm trying to make my own bad word filter, and it works great, exept when I have multiple words in a sentence. So now What it does is, it takes the values out of the database and then loops trugh it and must replace the ad words.
Let me say I have (in dutch) this sentence: Deze kanker zooi moet eens stoppen! Het is godverdomme altijd het zelfde zooitje.. These are the words that must been replaced in the database:
kanker and godverdomme.
So I have in my database:

Thats all good, exept when the 2 ords must been replaced, then it doesn't want to replace...
My helper function:
public static function Filter($value)
{
$badwords = DB::table('badwords')->get();
foreach ($badwords as $badword)
{
$replaced = str_ireplace($badword->bad_word, $badword->replacement, $value);
}
return $replaced;
}
Hope someone can help me :)
Kindest regards,
Robin
For each iteration in your foreach loop, you set the value of $replaced to whatever str_ireplace returns.
At the end of the function you will return a value where only the last bad word should have been replaced.
str_ireplace takes either a single string value or an array of string values to replace as first and second parameter which means that you instead could create two arrays, which you fill in theforeach loop and then run the str_ireplace after the loop, something like:
$words = [];
$replace = [];
foreach ($badwords as $badword) {
array_push($words, $badword->bad_word);
array_push($replace, $badword->replacement);
}
return str_ireplace($words, $replace, $value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With