View Full Version : Word Filter w/ Explode()
Coconut99 07-22-2003, 12:59 AM My guestbook's word filter doesn't work. I don't get any error messages, but the words aren't displayed as asterisks.
<?
$badwords = "word1,word2";
$filename = "data.html";
$message = stripslashes("$stuff");
$content = "Name: $name<BR>Email: <a href=\"\mailto:$mail\"\>$mail</a><BR>
Subject: $subject<BR>Message: $message<BR><BR>";
$filter = explode(",", $badwords);
foreach ($filter as $word) {
$message = str_replace("$word","****", $content);
}
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
print "Error!";
exit;
}
if (!fwrite($handle, $content)) {
print "Error!";
exit;
}
print "Success! Click <a href=\"view.php\">here</a> to go back. (You might need to refresh to see your post.)";
fclose($handle);
} else {
print "Error!";
}
?>
Thanks in advance!
christiandude03 07-22-2003, 03:37 PM Okay...I think I figured out where your problem is...
Your word replacer takes all the bad words in the variable $content and replaces them with stars, in the $message variable. Then, you ouput the unchanged $content variable to your file.
There are two ways to fix this:
1. Change $message = str_replace("$word","****", $content); to $content = str_replace("$word","****", $content);
OR
2. Change if (!fwrite($handle, $content)) { to if (!fwrite($handle, $message)) {
Hope this helps...
Coconut99 07-23-2003, 02:32 PM Thanks but that didn't work either. I'm trying a different tactic -- which doesn't work yet -- but I would be grateful if you would take a look at my new code. Thanks!
<?
$message = stripslashes("$stuff");
str_replace("word1", "****", $message);
str_replace("word2", "****", $message);
$content = "Name: $name<BR>Email: <a href=\"\mailto:$mail\"\>$mail</a><BR>
Subject: $subject<BR>Message: $message<BR><BR>";
$filename = "data.html";
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
print "Error!";
exit;
}
if (!fwrite($handle, $content)) {
print "Error!";
exit;
}
print "Success! Click <a href=\"guestbook.php\">here</a> to go back. (You might need to refresh to see your post.)";
fclose($handle);
} else {
print "Error!";
}
?>
Again, I don't get any parse errors or anything like that, but the words don't show up as asterisks.
Dude128 07-23-2003, 02:49 PM I think your problem there is that when you do the str_replace, $message isn't modified. you would have to reassign the result of the str_replace to $message for it to be affected. what you have now is basically saying "create a new string with the asterisks instead of the word, but then throw it away"
this should be better:
$message = str_replace("word1","****",$message);
Coconut99 07-23-2003, 05:53 PM Thanks! That worked. Is there any way to make the words I want filtered lowercase, but not the entire message without writing str_replace() with different capitals over and over? (So that wOrD1, Word1, word1, etc. would all be replaced with ****, but the rest of the message would stay the same.) Thanks again.
Dude128 07-23-2003, 06:28 PM I just found this in the php.net documentation:
instead of using str_replace(), use str_ireplace()
it is the case insensitive version of str_replace()
Coconut99 07-23-2003, 06:38 PM Thanks for the help, but my host is running PHP version 4.3.2, and php.net says that str_ireplace() requires PHP 5 CVS only. :( That would have been perfect, though. Do you know of any other way?
Dude128 07-23-2003, 06:44 PM oops, didn't even notice that.
if you look at the user comments on that page though, someone posted an alternative function that you could try instead.
Coconut99 07-23-2003, 07:30 PM Thanks a bunch! I'm using one of the commentor's functions which works on PHP 3 and 4. Everything works now!
christiandude03 07-24-2003, 06:33 AM Shouldn't you be able to use
eregi_replace ??
JamesyBaby 07-29-2003, 01:28 PM just use the function built for making words or strings lowercase:
strtolower();
:)
James.
Coconut99 07-31-2003, 03:10 AM Shouldn't you be able to use eregi_replace ??
Thanks, but since it works now I'm just keeping what I've got.
just use the function built for making words or strings lowercase:
strtolower();
I thought about using that function, but I was afraid it would make the entire message lowercase and I didn't want that. I thought it would just be simpler to use the other function.
Thanks anyway, though!
toosweet4u 08-04-2003, 03:16 AM Here's a function I wrote:
<?php
function filter_words($content) {
$badwords = @file('badwords.txt');
foreach ($badwords as $word) {
$word = substr(trim(strtolower($word)), 1);
$newword = str_repeat('*', strlen($word));
$content = preg_replace("/[^a-z]$word[^a-z]/i", $newword, $content);
}
return $content;
}
?>
Create a text file and put the banned words in there. (One per line.) Name it "badwords.txt"
badword
anotherbadword
someotherbadword
|