Location: PHPKode > scripts > Words Filter > words-filter/wordsfilter.class.php
class filter{
	$pattern = NULL,
	$replacement = NULL,
	$regexfilter = false,
	$special = array(		//all will be reversible
	'l'=>array('\|', '1'),
	public function __construct($filter, $maxspace=0, $sp=0, $b=false){
		/*use array(string=>replacement) as $filter
		filter with regex symbols is allowed but it's neccessary to use \ before each symbol
		if there're any regex, plz qoute the regex with ( and )
		$maxspace means when will be the filter been ignored after $maxspace speration(0=nerver)
		eg  $maxspace = 3, "sh    it" with 4 spaces can pass the filtering
		$sp: also detect similar characters such as "a" & "@"
		usage: array("a" = array("@"), charA = array(charB, charC, ..etc..), ..etc..)
		there's no need to set array("@"=>array("a")) after array("a"=>array("@")) because it'll be automatically done
		don't try to do that on ( or )
		$b: whether the programme will replace the word which is sticking to the others or not
		eg, if $b is false, the programme will detect the word "s hit" in "He's hitting me"*/
		if(!is_array($filter)) return false;
		$this->filter = $filter;
		$this->maxspace = $maxspace;
		if($sp!=0) $this->special = $sp;
		if($b) $this->border = '\b';
		return true;
	public function loadFilter($pr=NULL){
		/*set $pr=1 to rander a code of replacement patterns for further use(recommended)
		use $     ->loadFilter($yourcode) to load the code in the next time 
		in order to skip the process of randering the patterns & save time*/
			$pr = unserialize(base64_decode($pr));
			$this->pattern = $pr[0];
			$this->replacement = $pr[1];
		}elseif(!$this->pattern || !$this->replacement || !$this->s_replace){
			foreach($this->special as $o => $r){//create special matches, original to replacement
				$regex = "(".$o;
				foreach($r as $rs){//string to replacements
					$regex .= "|".$rs;
				$regex .= ")";
				$this->s_replace[$o] = $regex;
				for($i=0;$i<count($r);$i++) $this->s_replace[$r[$i]] = $regex;
			foreach($this->filter as $k => $v){//create replacing/matching regex pattern
				$slope = false;
				$inregex = 0;
				$len = strlen($k);
				$regex = "[^a-z0-9\\n]";
				$c = $this->maxspace>0?("{0,".$this->maxspace."}"):"*";
				while($len>1){//adding the regex from the end to the front
						$slope = false;
					}elseif(substr($k, $len-1, 1)=="\\"){//skip \
						$slope = true;
					$now = substr($k, $len, 1);
					if($now=="(") $inregex--;
					if($now==")") $inregex++;
							$k = substr($k, 0, $len).$regex.$c.$this->s_replace[$now].substr($k, $len+1);
							$k = substr($k, 0, $len).$regex.$c.substr($k, $len);
				$this->pattern[] = '/'.$this->border.$k.$this->border.'/i';
				$this->replacement[] = $v;
				if($inregex!=0) return false; //error of ( and )
		if($pr==1) return chunk_split(base64_encode(serialize(array($this->pattern, $this->replacement))));
		return true;
	public function filtering($str, $match=false){
		/*if $match==true, return:
		true: the string haven't been changed
		false: the string have been changed*/
		echo $this->pattern[0];
		$replaced = preg_replace($this->pattern, $this->replacement, $str);
		return $match?($str==$replaced):$replaced;
Return current item: Words Filter