Sorry if this has already been answered but I can't find an answer using my search queries and I don't know how else to put this. Basically, I'm building a word filter and I'm fetching the words from a db table and I'm using a for loop and preg_replace to replace the "bad words" but the "good word" is being prepended and appended to every word in the string. Here's my code:
$content = "The quick brown fox jumps over the lazy dog.";
// DB connection etc.
$bad = $row['bad_words'];
$good = $row['good_word']; // The "good word" for this example is "test"
$bad_words = explode(", ", $bad); // Let's say that "dog" is in the filter
$i = count($bad_words);
for($a=0;$a<=$i;$a++) {
$bad_words[$a] = str_replace(" ", "\s?", $bad_words[$a]); // I'm checking for a space, not sure if this bit is correct (\s?). Can I use the question mark in this case?
$content = preg_replace("/".$bad_words[$a]."\b/i", $good, $content);
}
The result is:
testThetest testquicktest testbrowntest testfoxtest testjumpstest testovertest testthetest testlazytest testtesttest.
The "bad word" is filtered but "test" is prepended ad appended to each word.