Trium918 Posted March 27, 2007 Share Posted March 27, 2007 I am trying to create an advance language filter. Ok, I am using the language filter that I got from phpfreaks tutorial, but it only check the words that are in the array $obscenities, curse, word etc.. I want it where it reads each character Example: c*u*r*s*e <? session_start(); function language_filter($string) { $obscenities = array("curse","word"," foul ","language"); 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 //header("Location: http://localhost/project4.php"); } else { //Content has value $info = $_POST['info']; $content = language_filter($_POST['content']); $fp = fopen("project4.txt", "a"); fwrite($fp, $content."\r\n"); fclose($fp); $_SESSION = 'isset'; header( "Location: " .$_SERVER['PHP_SELF'] ); //Have to go before any HTML } } ?> <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($_SESSION)) { //echo 'Form was posted'; $fp = fopen("project4.txt", "a"); $info = file('project4.txt'); for ($i = 0; $i < count($info); $i++) { echo '<br><hr>'; echo nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES)); } fclose($fp); unset($_SESSION); } ?> </td> </tr> <tr> <td colspan="2" align="center"><input type = "submit" value="Submit"></td> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
trq Posted March 27, 2007 Share Posted March 27, 2007 And what exactly are you stuck on? Quote Link to comment Share on other sites More sharing options...
Trium918 Posted March 27, 2007 Author Share Posted March 27, 2007 The user is not able to print curse or word but because the output is ***** and ****. The user is able to enter c*u*r*s*e and w*o*r*d and this is a problem. Quote Link to comment Share on other sites More sharing options...
trq Posted March 27, 2007 Share Posted March 27, 2007 Were not going to write it for you. Where are you stuck exactly? Quote Link to comment Share on other sites More sharing options...
AndyB Posted March 28, 2007 Share Posted March 28, 2007 Good luck. There is no hope that you could create a swearword filter that would keep out all swearwords when people can enter * or _ or - or | or .... in the middle. Accept the inevitable. People who don't know an adverb from a banana will swear. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 28, 2007 Share Posted March 28, 2007 what's a banana???? This is a regex issue and one that you can simply keep adding to. You could do something like creating an arry of words to filter and then using those in a reg ex to do the donkey work. <?php $filter = array('curse','swear','execrate'); $regex ='/'; foreach($filter as $key => $val) { $regex .='('; while($i < strlen($val)) { $regex .= '[^a-zA-Z0-9]*?'.$val{$i++}; } $regex .= '[^a-zA-Z0-9]?|'; } $regex .= substr($regex, 0, -1) . '/i'; $string = preg_replace($regex,'***',$string); Now that regex (if it even works) will do all kinds of wonderful and unexpected things probably. Have a play see how you get on. Quote Link to comment Share on other sites More sharing options...
Trium918 Posted March 28, 2007 Author Share Posted March 28, 2007 I donnot know where to start. thrope, where would you start just give me a starting point. Maybe I can build from that. My guess would be here: if (stristr(trim($string),$curse_word)) I understand that I need a function that would and a loop that reads each character even, and pull out the letters c u r s e. Notice that there are whitespace characters in this string. Quote Link to comment Share on other sites More sharing options...
dsaba Posted March 28, 2007 Share Posted March 28, 2007 a machine needs a pattern to spit things out, a method persay either you enlighten yourself on a similar pattern that people always use when they try to sneak in curse words OR you think up of all the different types of "sneaking in curse words" people will try, and incorporate that into your regex, for example you could think up that people might try to swear like these couple of ways: 1. fuck 2. f.u.c.k 3. f*u*c*k etc...., you can't just match the letters f, u, c, and k with regex because then it will find words that are in fact NOT curse words, and you can't simply filter out "fuck" because people might try to sneak it in so you are left with creating regexes for the most popular ways people might try to sneak those curse words in, this is not a simple task Quote Link to comment Share on other sites More sharing options...
Trium918 Posted March 28, 2007 Author Share Posted March 28, 2007 Ok, have anyone played Madden or any other online game? They have a great language filter system. I am not able to enter W*O*R*D if that word is being filter. The output would be like @%*$#!&. Every character is replace with another character. Is there a software that I can use. Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted March 28, 2007 Share Posted March 28, 2007 Regular Expressions are your best method of implementing a language filter. I recommend taking the time to read up on it, because you'll find many uses for it once you understand how powerful it is. There are hundreds of tutorials out there that will help you get a hang of it, including video tutorials: http://www.phpvideotutorials.com/regex/ Good luck! Quote Link to comment 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.