xyn Posted June 30, 2006 Share Posted June 30, 2006 Hi Guys,I was wondering how to add a word filter to my e-mail support form, say I had the following fields:NameEmailMessage How could I make a bad word filter to prevent swearing being applied into the "Name, e-mail and message" fields?thanks. Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/ Share on other sites More sharing options...
Barand Posted June 30, 2006 Share Posted June 30, 2006 [code=php:0]$badList = array('bad1', 'bad2', 'bad3');$text = 'I hate this bad1 tiny code when you use bad2 code tags';$clean = str_replace ($badList, '', $text);echo $clean;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51187 Share on other sites More sharing options...
Orio Posted June 30, 2006 Share Posted June 30, 2006 [code=php:0]//Lets say $text is the variable you want to scan for bad words$bad_words=array(...); //Put all the bad words this way: array(word1,word2,...)foreach($bad_words as $word){if(strpos($text,$word)!==FALSE){die("Error- please dont use the word".$word);};};[/code][hr]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51190 Share on other sites More sharing options...
xyn Posted June 30, 2006 Author Share Posted June 30, 2006 Thanks for the responces.But Is there anyway I can make a bad word list in MYSQL and extract each word into the array(); Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51195 Share on other sites More sharing options...
Orio Posted June 30, 2006 Share Posted June 30, 2006 You can, but I dont see that helpful ???If you still want:[hr][code=php:0]//Lets say $text is the variable you want to scan for bad words//connect to db here$query="SELECT badwords FROM badwords_table";$result=mysql_query($query);while($word=mysql_fetch_array($result)){if(strpos($text,$word["badwords"])!==FALSE){die("Error- please dont use the word".$word);};};[/code][hr]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51198 Share on other sites More sharing options...
obsidian Posted June 30, 2006 Share Posted June 30, 2006 [quote author=xyn link=topic=98993.msg389637#msg389637 date=1151672751]Thanks for the responces.But Is there anyway I can make a bad word list in MYSQL and extract each word into the array();[/quote]yes, that's one thing i like to do with my comment systems... i like to have a table with bad words and replacements, then, i simply run a function like this to clean the posts:[code]<?php// you must make your connection before running this functionfunction filterWords($String) { $sql = mysql_query("SELECT * FROM wordsTable"); $words = array(); $replace = array(); while ($x = mysql_fetch_array($sql)) { $words[] = $x['badWord']; $replace[] = $x['replacement']; } return str_replace($words, $replace, $String);}?>[/code]hope this helps! this is useful when you want to be able to control your filters from an admin panel since you can easily update your bad words table. Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51201 Share on other sites More sharing options...
xyn Posted June 30, 2006 Author Share Posted June 30, 2006 That was very helpful thanks,And I'm guessing where you add the words and replacements into the database it would look like[code=php:0] $words = array(badword1, etc.); $replace = array(replaceword1, etc.);[/code]correct? Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51205 Share on other sites More sharing options...
obsidian Posted June 30, 2006 Share Posted June 30, 2006 [quote author=xyn link=topic=98993.msg389648#msg389648 date=1151673570]That was very helpful thanks,And I'm guessing where you add the words and replacements into the database it would look like[code=php:0] $words = array(badword1, etc.); $replace = array(replaceword1, etc.);[/code]correct?[/quote]once you pull it out of the database, it is in those arrays, yes. to put new words in, you'd simply run a regular query:[code]INSERT INTO wordsTable (badWord, replacement) VALUES ('$myWord', '$myReplacement');[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51207 Share on other sites More sharing options...
xyn Posted June 30, 2006 Author Share Posted June 30, 2006 [quote author=obsidian link=topic=98993.msg389650#msg389650 date=1151673918][quote author=xyn link=topic=98993.msg389648#msg389648 date=1151673570]That was very helpful thanks,And I'm guessing where you add the words and replacements into the database it would look like[code=php:0] $words = array(badword1, etc.); $replace = array(replaceword1, etc.);[/code]correct?[/quote]once you pull it out of the database, it is in those arrays, yes. to put new words in, you'd simply run a regular query:[code]INSERT INTO wordsTable (badWord, replacement) VALUES ('$myWord', '$myReplacement');[/code][/quote]Yeah, And I could make a HTML form and use the $_POST[BadWord] & $_POST[Replace];As well...Also where do i put the following code?[code=php:0]<?php// you must make your connection before running this functionfunction filterWords($String) { $sql = mysql_query("SELECT * FROM wordsTable"); $words = array(); $replace = array(); while ($x = mysql_fetch_array($sql)) { $words[] = $x['badWord']; $replace[] = $x['replacement']; } return str_replace($words, $replace, $String);}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51208 Share on other sites More sharing options...
Orio Posted June 30, 2006 Share Posted June 30, 2006 It's a function, so you can put it everywhere you want. Best in the begining/end or in a include file.And you call it by:$something=filterWords($string) //when $string is the input (can called in any way, not just $string)Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51214 Share on other sites More sharing options...
obsidian Posted June 30, 2006 Share Posted June 30, 2006 [quote author=xyn link=topic=98993.msg389651#msg389651 date=1151674353]Also where do i put the following code?[/quote]it's just a function, so you need to add it somewhere (doesn't really matter where) on the page it will be used on, and to actually filter your words, all you've got to do is call the function:[code]$String = filterWords($String);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51215 Share on other sites More sharing options...
xyn Posted June 30, 2006 Author Share Posted June 30, 2006 hmm I'm slightly confused Now...If i shoved the function in the top of my page then I just put [code=php:0]$String = filterWords($String);[/code] before the e-mail is actually submitted? Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51217 Share on other sites More sharing options...
obsidian Posted June 30, 2006 Share Posted June 30, 2006 [quote author=xyn link=topic=98993.msg389660#msg389660 date=1151675015]hmm I'm slightly confused Now...If i shoved the function in the top of my page then I just put [code=php:0]$String = filterWords($String);[/code] before the e-mail is actually submitted?[/quote]that's about it 8) Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51223 Share on other sites More sharing options...
Orio Posted June 30, 2006 Share Posted June 30, 2006 Exactly.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51224 Share on other sites More sharing options...
xyn Posted June 30, 2006 Author Share Posted June 30, 2006 Just to double check, Would this be correct?[code=php:0]include("dir/db.php");$db = mysql_connect("localhost", $login, $pwd); //I dont need the or die();mysql_select_db("cpanel_dpanel"); //I don't need the or die();function filterWords($String) { $sql = mysql_query("SELECT * FROM filter"); $words = array(); $replace = array(); while ($x = mysql_fetch_array($sql)) { $words[] = $x['badword']; $replace[] = $x['replace']; } return str_replace($words, $replace, $String);}$msg="[b]BADWORD[/b], [b]BADWORD[/b] - Language filter testing";$to="[b]EMAIL[/b]";$sub="testing filter";$String = filterWords($String);mail( $to, $sub, $msg, "From: ash" );echo 'check inbox';exit;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51232 Share on other sites More sharing options...
Barand Posted June 30, 2006 Share Posted June 30, 2006 As the variable you want to filter is $msq, better change that to$msg = filterWords($msg); Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51238 Share on other sites More sharing options...
xyn Posted June 30, 2006 Author Share Posted June 30, 2006 [quote author=Barand link=topic=98993.msg389682#msg389682 date=1151677575]As the variable you want to filter is $msq, better change that to$msg = filterWords($msg);[/quote]Yepp That worked. Thank you!! =] Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51239 Share on other sites More sharing options...
xyn Posted June 30, 2006 Author Share Posted June 30, 2006 And I have one final questionwhere it has [code=php:0]$msg = filterWords($msg);[/code]can i use this to do multiple fields...[code=php:0]$msg = filterWords($msg, $name, $mail);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51262 Share on other sites More sharing options...
Barand Posted June 30, 2006 Share Posted June 30, 2006 Change the function to this, note the arguments as now passed "by ref" using "&"[code]<?phpfunction filterWords(&$w1, &$w2, &$w3) { $sql = mysql_query("SELECT * FROM filter"); $words = array(); $replace = array(); while ($x = mysql_fetch_array($sql)) { $words[] = $x['badword']; $replace[] = $x['replace']; } $w1 = str_replace($words, $replace, $w1); $w2 = str_replace($words, $replace, $w2); $w3 = str_replace($words, $replace, $w3); }?>[/code]and call withfilterWords($msg, $name, $mail); Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51273 Share on other sites More sharing options...
xyn Posted June 30, 2006 Author Share Posted June 30, 2006 That didn't work for me. My code is:[code=php:0] $mail = $_POST[email]; $name = $_POST[name]; $arti = $_POST[artist]; $song = $_POST[song];include("dPanel/db.php"); $db = mysql_connect("localhost", $login, $pwd); mysql_select_db("zroxxco_dpanel"); function filterWords(&$w1, &$w2, &$w3, &$w4) { $sql = mysql_query("SELECT * FROM filter"); $words = array(); $replace = array(); while ($x = mysql_fetch_array($sql)) { $words[] = $x['badword']; $replace[] = $x['replace']; } $w1 = str_replace($words, $replace, $w1); $w2 = str_replace($words, $replace, $w2); $w3 = str_replace($words, $replace, $w3); $w4 = str_replace($words, $replace, $w4); } filterWords($name, $mail, $arti, $song);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51298 Share on other sites More sharing options...
Orio Posted June 30, 2006 Share Posted June 30, 2006 [code=php:0] $mail = $_POST['email']; $name = $_POST['name']; $arti = $_POST['artist']; $song = $_POST['song'];include("dPanel/db.php"); $db = mysql_connect("localhost", $login, $pwd); mysql_select_db("zroxxco_dpanel"); function filterWords($w1, $w2, $w3, $w4) { $sql = mysql_query("SELECT * FROM filter"); $words = array(); $replace = array(); while ($x = mysql_fetch_array($sql)) { $words[] = $x['badword']; $replace[] = $x['replace']; } $return['1'] = str_replace($words, $replace, $w1); $return['2'] = str_replace($words, $replace, $w2); $return['3'] = str_replace($words, $replace, $w3); $return['4'] = str_replace($words, $replace, $w4); return $return;}$clean=filterWords($name, $mail, $arti, $song);$name=$clean['1'];$mail=$clean['2'];$arti=$clean['3'];$song=$clean['4'];[/code]That's how I'd do it.The only thing I dont understand is why you do this cleaning on the email... I mean, if someone calls himself this way, that's his problem =/Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51302 Share on other sites More sharing options...
xyn Posted June 30, 2006 Author Share Posted June 30, 2006 [quote author=Orio link=topic=98993.msg389750#msg389750 date=1151683602]The only thing I dont understand is why you do this cleaning on the email... I mean, if someone calls himself this way, that's his problem =/[/quote]What do you mean? & that didn't work either :S.I was trying to do a song requesting form, and trying to make sure there is no foul language used, I tried to check the fields before they where saved to the database but it doesn't seem to work :[ Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51309 Share on other sites More sharing options...
Orio Posted June 30, 2006 Share Posted June 30, 2006 Are there two cols called "badword" and "replace" in your filter table?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51314 Share on other sites More sharing options...
xyn Posted June 30, 2006 Author Share Posted June 30, 2006 yes. Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51316 Share on other sites More sharing options...
Orio Posted June 30, 2006 Share Posted June 30, 2006 Try changing the while in the function to:[hr][code=php:0]$i=0;while($x = mysql_fetch_array($sql)){$words[$i] = $x['badword'];$replace[$i] = $x['replace'];$i++;};[/code][hr]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13292-word-filter-arrays/#findComment-51322 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.