...

View Full Version : str_replace() problems



Taylor_1978
06-01-2004, 07:57 PM
Hi Folks,

In the message forum I have built, I have added str_replace to void out naughty words!!! (LOL)

Here is what I have:



$result = mysql_db_query($db, "SELECT * FROM words WHERE id>0");
while ($row = mysql_fetch_array($result)) {
$word = $row["word"];
$message = str_replace($word,"****",$message);
}


Problem is, lets say the value of $word = cat. If the $message has 'cat' in it then it replaces it with '****'. Problem is, if the message contains 'Cat' or 'cat!' etc it won't replace it!

Is there any way around this other than say making the entire message lower case.

Thanks in advance! :thumbsup:
Taylor.

sad69
06-01-2004, 10:24 PM
Try this code instead:


$result = mysql_db_query($db, "SELECT * FROM words WHERE id>0");
while ($row = mysql_fetch_array($result)) {
$word = $row["word"];
$message = preg_replace("/($word)/i","****",$message);
}


http://ca2.php.net/preg_replace

Sadiq.

g0g0l
12-05-2011, 06:17 AM
Or just use the case insensitive version of str_replace may be?



$result = mysql_db_query($db, "SELECT * FROM words WHERE id>0");
while ($row = mysql_fetch_array($result)) {
$word = $row["word"];
$message = str_ireplace($word,"****",$message);
}

BluePanther
12-05-2011, 06:31 AM
Way to revive a 7 year old thread! haha, but you do have a point - your solution to the problem was a lot better than preg_replace.

tangoforce
12-05-2011, 02:16 PM
They joined for that? - a 7 year old thread?.. well.. thats dedication i suppose!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum