DeanWhitehouse Posted November 13, 2008 Share Posted November 13, 2008 I have made this bad word filter, and i want it to be able to find the bad word whether it is in lowercase (e.g. f*ck), uppercase (e.g. F*CK) or a mixture (e.g. F*cK) I want to do this without changing the comment (it's a comment system) to lowercase or uppercase. This is how i tried it <?php $bad = array('removed for younger/more sensitive readers. In summary it contained a list of banned words, with variations. Some in mixed cases'); if(isset($_POST['Go!'])) { $name = make_safe($_POST['name']); $message = make_safe($_POST['msg']); foreach($bad as $low) { $bader[] = strtolower($low); } foreach($bad as $high) { $worse[] = strtoupper($high); } foreach($bad as $first) { $omg[] = ucfirst($first); } $name = str_replace($bader,"*****",$name); $name = str_replace($worse,"*****",$name); $name = str_replace($omg,"*****",$name); $name = str_replace($bad,"*****",$name); $message = str_replace($bad,"*****",$message); $message = str_replace($bader,"*****",$message); $message = str_replace($worse,"*****",$message); $message = str_replace($omg,"*****",$message); echo $name."<br>".$message; } ?> <form action="" method="post"> Name <input type="text" name="name" id="nme" maxlength="20" size="15" value="<?php if(isset($_POST['nme'])){echo $_POST['nme'];}?>" ><br> Message <p><textarea rows="5" cols="53" name="msg" id="txt"><?php if(isset($_POST['msg'])){echo $_POST['msg'];}?></textarea></p> <p class="links"><input type="submit" value="Submit" name="Go!" id="x" > <input type="reset" value="Reset" name="reset" id="x"></p> </form> Now as you can tell this is not the most efficient way of doing it, what would you advise? EDIT: This is only a small part of the code. Quote Link to comment https://forums.phpfreaks.com/topic/132622-solved-php-bad-word-filter/ Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 how about getting rid of all those foreach loops and using str_ireplace instead? Quote Link to comment https://forums.phpfreaks.com/topic/132622-solved-php-bad-word-filter/#findComment-689628 Share on other sites More sharing options...
DeanWhitehouse Posted November 13, 2008 Author Share Posted November 13, 2008 Ah, yep , i always see it when i am looking at string functions but never use it. Will that sort out the problem of F*cK? Quote Link to comment https://forums.phpfreaks.com/topic/132622-solved-php-bad-word-filter/#findComment-689642 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 it is a case insensitive search. It will match Suck sucK SucK SuCk SUCK suck but not a literal *'s instead of letters or spaced out letters or special chars that look like the letters, etc.. s*ck su ck svck etc... Quote Link to comment https://forums.phpfreaks.com/topic/132622-solved-php-bad-word-filter/#findComment-689645 Share on other sites More sharing options...
DeanWhitehouse Posted November 13, 2008 Author Share Posted November 13, 2008 Yeah, i don't think i will need to worry about the other words with spaces etc. If i decide to i will need to write a regex i think. Quote Link to comment https://forums.phpfreaks.com/topic/132622-solved-php-bad-word-filter/#findComment-689647 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.