0

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.

1 Answer 1

1

I've tried your example, and seems error in this row

for($a=0;$a<=$i;$a++) {

should be like this

for($a=0;$a<$i;$a++) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.