php - Laravel bad wordfilter as an helper -
i'm trying make own bad word filter, , works great, exept when have multiple words in sentence. is, takes values out of database , loops trugh , must replace ad words.
let me have (in dutch) sentence: deze kanker zooi moet eens stoppen! het godverdomme altijd het zelfde zooitje.
. these words must been replaced in database:
kanker
, godverdomme
.
so have in database:
thats good, exept when 2 ords must been replaced, doesn't want replace...
my helper function:
public static function filter($value) { $badwords = db::table('badwords')->get(); foreach ($badwords $badword) { $replaced = str_ireplace($badword->bad_word, $badword->replacement, $value); } return $replaced; }
hope can me :)
kindest regards,
robin
for each iteration in foreach loop, set value of $replaced
whatever str_ireplace
returns.
@ end of function return value only last bad word should have been replaced.
str_ireplace
takes either single string value or array of string values replace first , second parameter means instead create 2 arrays, fill in theforeach
loop , run str_ireplace
after loop, like:
$words = []; $replace = []; foreach ($badwords $badword) { array_push($words, $badword->bad_word); array_push($replace, $badword->replacement); } return str_ireplace($words, $replace, $value);
Comments
Post a Comment