Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
bad words filter failing
Old 09-16-2008, 01:26 AM bad words filter failing
Skilled Talker

Posts: 69
Trades: 0
Hey,

I am writing a bad words filter but it is not filtering any bad words. Can anyone help?

Code:
function filter($string) {
$badwords=file("badwords.txt");
for ($i=0;$i < count($badwords);$i++){
$string = str_replace($badwords[$i],str_repeat("*",strlen($badwords[$i])),$string);
}
return $string;
} 

$comments = filter($comments);
Thanks,
Matt
__________________
Matt
webmaster of
Please login or register to view this content. Registration is FREE
- Online wedding planning
weddingm is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-16-2008, 03:15 AM Re: bad words filter failing
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
It does not work because file() returns an array of strings where each string ends with a newline character. Obviously your input text does not contain a newline after aech bad word. You should trim() each line of your txt file either right after file() or before str_replace().

But I would better use preg_replace() because it gives you much much more flexibility.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 09-16-2008, 11:43 PM Re: bad words filter failing
Skilled Talker

Posts: 69
Trades: 0
I have 1 bad word per line in the text file. The code is still not filtering out the bad words with a * per character. Any suggestions.

I am using the following code:

$er=0;

Code:
function filter($comments) {
$badwords=file("badwords.txt");
for ($i=0;$i < count($badwords);$i++){
$comments = preg_replace(trim($badwords[$i]),str_repeat("*",strlen($badwords[$i])),$comments);
}
return $comments;
} 
if ($er=0) { $comments=filter($comments);}
__________________
Matt
webmaster of
Please login or register to view this content. Registration is FREE
- Online wedding planning
weddingm is offline
Reply With Quote
View Public Profile
 
Old 09-17-2008, 02:37 AM Re: bad words filter failing
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Are you trying to extend the code from http://www.webmaster-talk.com/php-fo...ds-filter.html ?

Anyway,
PHP Code:
$comments preg_replace(trim($badwords[$i]),str_repeat("*",strlen($badwords[$i])),$comments); 
should be
PHP Code:
$comments preg_replace('/'.preg_quote(trim($badwords[$i]),'/').'/i',str_repeat("*",strlen($badwords[$i])),$comments); 
Though your modifications are a bit less efficient than your earlier thread.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 09-17-2008, 02:40 AM Re: bad words filter failing
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
OMG
PHP Code:
    $badwords = array('bad''word');

    
$comments "this is a bad phrase that has bad, very bad words";

    echo 
$comments."\n";

    foreach (
$badwords as $bw) {
        
$comments preg_replace("!\b{$bw}\b!"str_repeat("*",strlen($bw)), $comments);
    }

    echo 
$comments."\n"
You did not set delimiter for the regular expression (two exclamation marks in my case, but you can use any characters like / / or @ @). Also look at \b modifiers which mean "match only word boundaries" so that "badminton" or "sword" stayed untouched.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to bad words filter failing
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14813 seconds with 12 queries