Hi,

I built a comments system,

basically the comments appear underneath the form once submitted and page is refreshed, everything works like it should.

I have this piece of code below, what i am having difficulty in is first time it worked then it no longer works.

<?php

$bannedwordquery = mysql_query( "SELECT * FROM `post_comment_banned_words`");
while( $row = mysql_fetch_array( $bannedwordquery ) ) {
$replacementword = "banned word";
$comment = str_ireplace($row['banned_words'], $replacementword, "".$_POST['comment']."" );
}

?>

for some reason it worked first time and words in DB came up as banned word like it suppose to and now it does not filter the banned words, i did not change anything since creating it earlier when it was working hence why i am so confused.

Can anyone tell me what might be the problem is please?

here is my DB table for the banned words:

- Table structure for table `post_comment_banned_words`
--

CREATE TABLE IF NOT EXISTS `post_comment_banned_words` (
  `id` int(11) NOT NULL auto_increment,
  `banned_words` varchar(15) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=66 ;

I am baffeled.

Thanks
GUK

replace line 6 , wih below -

echo  $comment = str_ireplace($row['banned_words'], $replacementword, "".$_POST['comment']."" );

or in simple language echo the $comment and post the ouput here

Hey.

The problem is that you do the replacement one word at a time, inside the loop.

This means that your $comment variable is overwritten for every word, and each time with only that word replaced. So you end up with the original comment text with just the last word in the database replaced.

You should create an array of words and replace them all at the same time. And, furthermore, to avoid mangling valid words, like replacing the "ass" in "assessment", you could use a regular-expression, rather then the str_ireplace function.

Try something like:

// Fetch all the words and put them in an array.
$sql = "SELECT `banned_words` FROM `post_comment_banned_words`";
$result = mysql_query($sql) or die(mysql_error());
$bannedWords = array();
while($row = mysql_fetch_array($result)) {
	$bannedWords[] = $row['banned_words'];
}

// Replace the words using a regular-expression.
$pattern = '/\b'. implode($bannedWords, "\b|\b") .'\b/i';
$replacement = "***";
$comment = preg_replace($pattern, $replacement, $_POST['comment']);

Hey.

The problem is that you do the replacement one word at a time, inside the loop.

This means that your $comment variable is overwritten for every word, and each time with only that word replaced. So you end up with the original comment text with just the last word in the database replaced.

You should create an array of words and replace them all at the same time. And, furthermore, to avoid mangling valid words, like replacing the "ass" in "assessment", you could use a regular-expression, rather then the str_ireplace function.

Try something like:

// Fetch all the words and put them in an array.
$sql = "SELECT `banned_words` FROM `post_comment_banned_words`";
$result = mysql_query($sql) or die(mysql_error());
$bannedWords = array();
while($row = mysql_fetch_array($result)) {
	$bannedWords[] = $row['banned_words'];
}

// Replace the words using a regular-expression.
$pattern = '/\b'. implode($bannedWords, "\b|\b") .'\b/i';
$replacement = "***";
$comment = preg_replace($pattern, $replacement, $_POST['comment']);

Hi,

Thanks so much it works like it should do, yeah regular expressions is something i want to master soon as it is so valuable, i did start to learn php about 8 months ago, but after about two months had allot of family problems so only learnt for to months and had to give it up for six months, but now everything is back to normal i can start learning again. But i see what you mean and where i went wrong. This is why it must have worked first time when there was only one word in DB. But anymore it is replaced.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.