Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 remove all bad words from form
Index -> Programming, PHP -> PHP Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
alpesh




PostPosted: Thu Nov 01, 2007 2:07 am   Post subject: remove all bad words from form

Quote:
To remove all bad words from form
-------------

<?php
function Filter(&$text, $replace)
{
//fill this array with the bad words you want to filter and their replacements
$bads = array (
array("Fool","F**l"),
array("crap","c***")
);

if($replace==1)
{
//we are replacing
$remember = $text;

for($i=0;$i<sizeof($bads);$i++)
{
//go through each bad word
$text = eregi_replace($bads[$i][0],$bads[$i][1],$text); //replace it
}

if($remember!=$text) return 1; //if there are any changes, return 1

} else {

//we are just checking
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
}

}
}

?>


Example1.php

<?php

include('Filter.php');

// This will replace all bad words with their replacements.$any is 1 if it found any
$result = Filter($wordsToFilter,1);

//this will not repace any bad words but instead will only search for them. $any is 1 if it found any
$result = Filter($wordsToFilter,0);

?>
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Nov 01, 2007 3:14 am   Post subject: Re: remove all bad words from form

Is "pointer" a bad word?

alpesh @ Thu Nov 01, 2007 2:07 am wrote:

function Filter(&$text, $replace)
...
$remember = $text;
...
if($remember!=$text) return 1; //if there are any changes, return 1

$remember == $text, so it will never != $text unless, of course, you change $remember. You're not doing that.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Mazer




PostPosted: Thu Nov 01, 2007 8:09 am   Post subject: RE:remove all bad words from form

For fuck's sake, why couldn't you use some goddamn code tags, you jackass.

EDIT: Tony, he's not changing $remember, but it looks like he is changing $text.
Zampano




PostPosted: Thu Nov 01, 2007 8:20 am   Post subject: Re: remove all bad words from form

I really want tho see this happen: You are all crap fools, the whole bunch of ya! Shocked Smile
Tony




PostPosted: Thu Nov 01, 2007 11:33 am   Post subject: Re: RE:remove all bad words from form

Mazer @ Thu Nov 01, 2007 8:09 am wrote:
EDIT: Tony, he's not changing $remember, but it looks like he is changing $text.

$remember is a pointer, same as $text. While the contents of what one of them points to changes, both variables still point to the same thing, and thus will always equal each other.

code:

function Baz(&$foo){
   $bar = $foo;
   echo $bar == $foo; // it's 1
   $foo = "something totally different";
   echo $bar == $foo; // it's still 1
}
$text = "some text";
Baz($text);
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
octopi




PostPosted: Thu Nov 01, 2007 12:13 pm   Post subject: Re: remove all bad words from form

$remember isn't a pointer, if it was a pointer(reference) then the line would be

code:
$remember =& $text;


$remember is simply assigned the value of $text (which is really the value of $wordsToFilter) and it's address is NOT mapped to the same address as $text;

to clarify, $remember grabs the string value out of memory which is referenced by $text, and stores a copy of it in a different location, which is then referenced by $remember.

code:

$foo = "hello";
$text = "some text";

$remember = $text;
$remember = "not some text anymore";

//$text will still be "some text"

$remember =& $foo;
$remember = "goodbye";

echo $foo;  //would output goodbye


It doesn't matter that the original text is a reference to another variable/memory location, because we haven't told $remember to take on the reference, we've just told it to take on the value (php works differently than other languages)
Tony




PostPosted: Thu Nov 01, 2007 3:25 pm   Post subject: Re: remove all bad words from form

octopi @ Thu Nov 01, 2007 12:13 pm wrote:
php works differently than other languages

heh. apparently.

I stand corrected. I should stop pretending that I understand PHP.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Mazer




PostPosted: Thu Nov 01, 2007 6:55 pm   Post subject: Re: remove all bad words from form

Tony @ Thu Nov 01, 2007 3:25 pm wrote:
I should stop pretending that I understand PHP.

Me, too, but not until I'm actually found out Wink
Sponsor
Sponsor
Sponsor
sponsor
NickPresta




PostPosted: Sun Nov 04, 2007 5:59 pm   Post subject: RE:remove all bad words from form

I wrote a PHP word filter a few months ago and it seems to get the job done.

It is fairly simple right now, it replaces the "banned" word(s) with asterisks and has strict or loose filtering.

http://nickpresta.ath.cx/h4x/wordfilter/index.php
Display posts from previous:   
   Index -> Programming, PHP -> PHP Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: