Current location: Hot Scripts Forums » Programming Languages » PHP » filter bad words -- help with script


filter bad words -- help with script

Reply
  #1 (permalink)  
Old 06-26-06, 03:17 AM
aprogrammer aprogrammer is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
filter bad words -- help with script

Here's my current script but it is not working.
can someone help me fix? I want it to work also regardless of uppercase or lowercase.

PHP Code:

<?php


$filtered
=0

foreach(
$_POST as $val

echo 
$val;

$filteredBadWordFilter($val); 

}  

echo 
"filtered:".$filtered;


function 
BadWordFilter($text)
{
    
//fill this array with the bad words you want to filter and their replacements
    
$bads =  array ("test","badword");
    
        for(
$i=0;$i<sizeof($bads);$i++) {            //go through each bad word
            
if(eregi($bads[$i][0],$text))return 1//if we find any, return 1
        
    
        
}    
        
    }



?>


<form method=post action=testbadword.php>
<textarea name=test>
</textarea>
<input type=submit name=submit value=sumbit>
</form>
Reply With Quote
  #2 (permalink)  
Old 06-26-06, 03:47 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
If you've got PHP 5 you can use str_ireplace()

PHP Code:

<?php


if (isset($_POST['test']))
{
    
$val $_POST['test'];
    
    
$replacements = array
    (
        
'test' => 'good word',
        
'bad word' => 'other good word',
        
'blah' => 'yeh'
    
);
    
    
$val str_ireplace(array_keys($replacements), array_values($replacements), $val);
    
    echo 
$val;
}

?>
What exactly do you want to do? Count the bad words in the submitted text, or actually replace them?

If you only want to count them try this

PHP Code:

if (isset($_POST['test']))

{

       echo 
'Matches: 'BadWordFilter($_POST['test']);
    
}


function 
BadWordFilter($text)
{
    
//fill this array with the bad words you want to filter and their replacements
    
$bads =  array ("test","badword");
    
$matches 0;
        
    foreach (
$bads as $bad)
    {            
//go through each bad word
        
if (eregi($bad$text))
        
$matches++; 
    }
    
    return 
$matches;

Reply With Quote
  #3 (permalink)  
Old 06-26-06, 04:27 AM
aprogrammer aprogrammer is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
thanks for your help.
I edited your version a bit but it's still not working 100%
Basically waht I want is to check for the badwords and if a badword is found I set $filtered=1
can you help me fix. I see the variable matches is changing but when I tried to set filter it's not working. what am i doing wrong?


PHP Code:

<?php


$filtered
=0
$matches=0;
foreach(
$_POST as $val

echo 
$val;

//echo 'Matches: '. BadWordFilter($val); 
$matches=BadWordFilter($val); 
echo 
$matches;
}  

if (
$matches!=0
{
$filtered=1;
echo 
"should be filtered<bR>";
}

echo 
"filtered:".$filtered;




       
     



function 
BadWordFilter($text

    
//fill this array with the bad words you want to filter and their replacements 
    
$bads =  array ("test","badword"); 
    
$matches 0
         
    foreach (
$bads as $bad
    {            
//go through each bad word 
        
if (eregi($bad$text)) 
        
$matches++;  
    } 
     
    return 
$matches
}  

?>


<form method=post action=testbadword.php>
<textarea name=test>
</textarea>
<input type=submit name=submit value=sumbit>
</form>

Last edited by nico_swd; 06-26-06 at 05:41 AM. Reason: [php] wrappers
Reply With Quote
  #4 (permalink)  
Old 06-26-06, 06:04 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Can I may ask why you're using a loop to catch the value? Why not using $_POST['test'] instead?

This sets $filtered to 1 if a bad word is found, and to 0 if not.

PHP Code:

<?php


if (isset($_POST['test']))
{
    
$filtered BadWordFilter($_POST['test']) > 0;
    
    echo 
'Filtered: '$filtered;
}

function 
BadWordFilter($text

    
//fill this array with the bad words you want to filter and their replacements 
    
$bads =  array ("test","badword"); 
    
$matches 0
         
    foreach (
$bads as $bad//go through each bad word 
    
{            
        if (
eregi($bad$text)) 
        
$matches++;  
    } 
     
    return 
$matches
}  

?>
Reply With Quote
  #5 (permalink)  
Old 06-26-06, 06:46 AM
aprogrammer aprogrammer is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
but i'm looping through all the values of the form and if any are filtered then it should be set to fitered -- will this do that?

I don't want it to go by the last form field. (in this demo there is ony one field but I will be using this on a form with many fields)
Reply With Quote
  #6 (permalink)  
Old 06-26-06, 07:30 AM
aprogrammer aprogrammer is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
i also want this to work if someone puts in the text box this is a test then test should be filtered. can you help me fix this?
Reply With Quote
  #7 (permalink)  
Old 06-26-06, 10:43 AM
aprogrammer aprogrammer is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
figured it out - thanks for your help
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Raffle/Lottery Script (Very profitable!), Coded it myself. Voltaire General Advertisements 6 03-16-09 07:15 AM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 10:17 AM
Raffle/Lottery Script (Very profitable!), Coded it myself. Voltaire General Advertisements 2 01-02-06 11:55 PM
excluding words from smut filter Skeleton Man Perl 7 01-02-05 09:43 AM


All times are GMT -5. The time now is 07:18 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.