Oct
10
PHP Regex - Finding Variations on Bad Words
October 10, 2007 |
If evil people type something like ’stupid fuck’, obviously, it will be detected and blocked by most sites, so they become smart, they adapt, so they may use ‘fuk’ instead of ‘fuck’. They don’t care about the spelling, all they want is to let you know they are trying to say ‘fuck’. We definitely understand, but does our little PHP program understand? Well, that depends on how we-engineers program it. Here is a simple way to stop evolving ‘evil doers’ who try to post mutant bad words on our site. Enjoy!
Regular Expression Explanation:
| stupid | the word ’stupid’, followed by … |
| space | a space, followed by … |
| fu | the letteers ‘fu’, followed by … |
| c | c, which may … |
| ? | appear once, but isn’t required, followed by … |
| k | the letter ‘k’, followed by … |
| (er) | a group that contains the letters ‘er’ … |
| ? | that may appear once, but isn’t require |
Source Code:
<br />
<html><br />
<head><br />
<title>Finding Variations on phrase</title><br />
</head><br />
<body><br />
<form action="<?php $_SERVER['PHP_SELF'] ?>"<br />
method="post"><br />
<input type="text" name="str"<br />
value="<?php print $_POST['str'];?>" /><br /><br />
<input type="submit" value="stop bad words" /><br />
<br /><br /><br />
<?php<br />
if ( $_SERVER['REQUEST_METHOD'] == "POST" )<br />
{<br />
$str = $_POST['str'];<br />
if ( preg_match( "/stupid fuc?k(er)?/", $str ) )<br />
{<br />
print "<b>Found him: '". $str . "'</b><br/>";<br />
}<br />
else<br />
{<br />
print "<b>Did NOT find match</b><br/>";<br />
}<br />
}<br />
?><br />
</form><br />
</body><br />
</html><br />
Similar Posts
- PHP Regex - Finding Multiple Dirty Words
- PHP Regex - Finding the F Word
- PHP Regex - Bad Words Streamlining
Comments
2 Comments so far



































(”(f+u*c*k+) || (shi*y*t)”,”Bad word”, $anystring) will this find fuck fuk fk fck and shit shyt shiyte ??? i mean is the syntax of the pattern above correct???
Two things to note:
1. You should use one single ‘|’ metacharacter as an ‘or’ operator in regular expressions.
So || should be |.
2. If you are using preg(PCRE-Perl compatible), notice that the regular expressions in the first parameter must start and end with a delimiter ‘/’, just like the Perl expressions.
(”/(f+u*c*k+)|(shi*y*t)/”,”Bad word”,$anystring)
The rest parts seems fine to me. :)