Portfolio
Scripts and Websites I've made using PHP and MySQL mainly.
Snippets
Code snippets and mini tutorials
Contact
Contact me if you have any questions or queries
Home / Snippets / word_filter
This section is new and undergoing snippet addition, as of 13/6/11
You may want to add in a swear filter to your website, or just filter some words for the hell of it.
In any case, this is quite easily done, simply use the code below.
Add the words you want to filter to the blacklist array.
You can then use it like so:
This section is new and undergoing snippet addition, as of 13/6/11
Word Filter
You may want to add in a swear filter to your website, or just filter some words for the hell of it.
In any case, this is quite easily done, simply use the code below.
Add the words you want to filter to the blacklist array.
<?php
class WordFilter
{
private $blacklist = array();
public function __construct()
{
$this->blacklist[] = "bad";
$this->blacklist[] = "verynaughty";
$this->blacklist[] = "naughty";
}
public function filter($txt)
{
for($w = 0; $w < count($this->blacklist); $w++)
{
$rep = "";
for($i = 0; $i < strlen($this->blacklist[$w]); $i++)
{
$rep .= "*";
}
$txt = str_ireplace($this->blacklist[$w], $rep, $txt);
}
return $txt;
}
}
?>
You can then use it like so:
$str = "this is a sentence with some bad and some verynaughty words in, as well as some naughty ones."; $myFilter = new WordFilter(); $str = $myFilter->filter($str);
Comments
No Comments Have Been Made


