Oct
12
PHP Regex - Bad Words Streamlining
October 12, 2007 |
So what if we detect and filter out all bad words on the site? Will that make the bad guys try to behave themselves or go to hell? No, they will neither try to behave themselves nor go to hell. What they will do is to leave our website and go visit another website that accepts bad words. So if you filter out all bad words on the site, go to visit your google analytics tomorrow, you will see the graphics change from a slash shape to a mountain shape. We don’t want to see that, so how? So instead of filtering out bad words, we should ’streamline’ them. Instead of filtering out the word ‘fuck’, we can streamline it to ‘f**k’. In this way, we can keep both our ‘moral standard’ and visitors on our site. Hypocritical it might be, but so what? We need traffic!
Below is how I use PHP’s preg_replace to make the streamline process work like a charm:
Regular Expression Explanation:
| \b | a word boundary, followed by … |
| fuck | the word ‘fuck’, followed by … |
| \b | a word boundary. |
Source Code:
<html>
<head><title>Replacing Words</title></head>
<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>"
method="post">
<input type="text" name="value"
value="<?php print $_POST['value'];?>" />
<br />
<input type="submit" value="Replace word" />
<br /><br />
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
$str = $_POST['value'];
$newstr = preg_replace(
"/\bfuck\b/", "f**k", $str
);
print "<b>$newstr</b><br />";
}
?>
</form>
</body>
</html>
Similar Posts
- PHP Regex - Finding Multiple Dirty Words
- PHP Regex - Finding the F Word
- PHP Regex - Finding Variations on Bad Words
Comments
3 Comments so far



































I just want to ask is there difference when using ereg or preg in defining pattern or pattern syntax is the same in both cases it will be nice to hear someones reply …
Hi, alihammad. preg is much faster than ereg. Although the difference is not obvious when you do a single test, but if you are running a website that handles hundreds of filtering calls per second, the difference can be significant. So I strongly recommend you to use preg. hope this helps. :)
thanks!