Trium918 Posted March 23, 2007 Share Posted March 23, 2007 Is there a php software that fliters out bad language and other unwanted characters, so that I donnot have to create one from scratch? Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/ Share on other sites More sharing options...
Hell Toupee Posted March 23, 2007 Share Posted March 23, 2007 There probably are plenty of scripts that have it, but there's no built in function as such...you'd be best to create an array of all the words you wanted to filter out, then loop through that array, checking the content of the string you wish to censor for a match each time. Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213607 Share on other sites More sharing options...
cmgmyr Posted March 23, 2007 Share Posted March 23, 2007 actually there is already one on the site: http://www.phpfreaks.com/tutorials/122/0.php ...why don't you just make your own? Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213608 Share on other sites More sharing options...
rcorlew Posted March 23, 2007 Share Posted March 23, 2007 I have been working on just the same thing for the last day with some success until I stopped handling words as absolute array values since people could change capitalization within words and the like. So here is what I made, it is pretty simple but it catches all variants. I assume you have mySql and know how to connect. <?php //Set up string to have all your variables to be parsed seperated only by a regular space no commas $p_string = "$pa $pb $pc $pd"; //Split all posted words into single words to compare against bannned words $a11 = preg_split("/[\s,]+/", $p_string, -1, PREG_SPLIT_NO_EMPTY); foreach($a11 as $z21); $con = mysql_connect($HOST, $USER, $PASS); if (!$con) { die("Error connecting to MySQL database!"); } mysql_select_db($NAME, $con); //Here I query the 'table' for the 'column' $query = mysql_query("SELECT * FROM badwords WHERE cussword LIKE '%$z21%'"); $num_rows = mysql_num_rows($query); if ($num_rows >= "1") { $message = "<h3 align='center'>This is a place for communicating, please rethink what you are submitting and try again</h3>"; //Here I unset all variables to fail the next validation and make them fill the form out again unset($pa, $pb, $pc, $pd); } ?> I know that the a11 and z21 looks weird but I was having intermittent problems with using such sort variable names so I just added 1. The real functionality of this comes from the sql query 'LIKE' wich will match words without being case sinsitive. Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213619 Share on other sites More sharing options...
Trium918 Posted March 23, 2007 Author Share Posted March 23, 2007 I have a text file that I want to filter out all the bad language. I could use a database but in this case I am using a text file. Every line of code below works, but I have no way to filter it. Expamle of this code <form method="POST"> <table border="1" align="center" width="300"> <tr> <td colspan="2" align="center">How to Change the World?</td> </tr> <tr> <td align="center"><img src="../images/atlas.jpg" width="100" height="165" /></td> <td><textarea name = "content" cols="50" rows="10"></textarea></td> </tr> <tr> <td colspan="2"> <? if(isset($_POST['content'])) { //Page was submitted if (!$_POST['content']) { //value of content is empty $output = "There was no data entered!"; echo $output; } else { //Content has value $content = $_POST['content']; $info = $_POST['info']; @$fp = fopen("project4.txt", "a"); fwrite($fp, $content."\r\n"); $info = file('project4.txt'); //Array $info = array("curse","word"," foul ","language"); for ($i = 0; $i < count($info); $i++) { echo '<br><hr>'; echo nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES)); } fclose($fp); } } ?> </td> </tr> <tr> <td colspan="2" align="center"><input type = submit name="submit"></td> </tr> </table> </form> This is the code that I got from phpfreaks tutorial but I have no idea on how to insert this into my code where it will work. function language_filter($string) { $obscenities = @file("path/to/your/file/foul_language.txt"); foreach ($obscenities as $curse_word) { if (stristr(trim($string),$curse_word)) { $length = strlen($curse_word); for ($i = 1; $i <= $length; $i++) { $stars .= "*"; } $string = eregi_replace($curse_word,$stars,trim($string)); $stars = ""; } } return $string; } Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213684 Share on other sites More sharing options...
psychohagis Posted March 23, 2007 Share Posted March 23, 2007 Just use eregi_replace() Heres a function for you: function swear_remover($text) { $text = eregi_replace('swear', '****', $text); $text = eregi_replace('swear', '****', $text); $text = eregi_replace('swear', '****', $text); return $text; } Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213709 Share on other sites More sharing options...
Trium918 Posted March 23, 2007 Author Share Posted March 23, 2007 how would I place that into my code? Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213719 Share on other sites More sharing options...
Trium918 Posted March 23, 2007 Author Share Posted March 23, 2007 I still code use some help. Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213781 Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 $txt = "I swear that this swear filter is swearing me dead!"; $txt = swear_remover($txt); function swear_remover($text) { $text = eregi_replace('swear', '****', $text); $text = eregi_replace('swear', '****', $text); $text = eregi_replace('swear', '****', $text); return $text; } Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213783 Share on other sites More sharing options...
Trium918 Posted March 23, 2007 Author Share Posted March 23, 2007 frost110 I understand how to do it like that, but I am having problems putting that in my code. Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213798 Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 <form method="POST"> <table border="1" align="center" width="300"> <tr> <td colspan="2" align="center">How to Change the World?</td> </tr> <tr> <td align="center"><img src="../images/atlas.jpg" width="100" height="165" /></td> <td><textarea name = "content" cols="50" rows="10"></textarea></td> </tr> <tr> <td colspan="2"> <? function language_filter($string) { $obscenities = @file("path/to/your/file/foul_language.txt"); foreach ($obscenities as $curse_word) { if (stristr(trim($string),$curse_word)) { $length = strlen($curse_word); for ($i = 1; $i <= $length; $i++) { $stars .= "*"; } $string = eregi_replace($curse_word,$stars,trim($string)); $stars = ""; } } return $string; } if(isset($_POST['content'])) { //Page was submitted if (!$_POST['content']) { //value of content is empty $output = "There was no data entered!"; echo $output; } else { //Content has value $content = language_filter($_POST['content']); $info = $_POST['info']; @$fp = fopen("project4.txt", "a"); fwrite($fp, $content."\r\n"); $info = file('project4.txt'); //Array $info = array("curse","word"," foul ","language"); for ($i = 0; $i < count($info); $i++) { echo '<br><hr>'; echo nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES)); } fclose($fp); } } ?> </td> </tr> <tr> <td colspan="2" align="center"><input type = submit name="submit"></td> </tr> </table> </form> There you go. Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213801 Share on other sites More sharing options...
Trium918 Posted March 23, 2007 Author Share Posted March 23, 2007 Your code is displaying the array. I am not able to enter any data. Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213828 Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 My code, I didn't write that function. Anyhow if it is display an array, is $_POST['content'] that an array? If it is you need to foreach through it and apply the language_filter to that. Quote Link to comment https://forums.phpfreaks.com/topic/43997-solved-flitering-out-bad-language/#findComment-213856 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.