Location: PHPKode > scripts > Simple Word Filter > Simple-Word-Filter-master/word-filter.php
<h1 style="margin:0;">Simple word filter</h1>
<h2 style="margin:0;">by <a href='http://www.linkedin.com/in/elabid' target="_blank">Mohammad El-Abid</a></h2>
<h3 style="margin:0;">of <a href='http://isscary.com' target="_blank">IsScary.Com</a></h3>
<hr />
<?php

function is_vector_array($var) { 
	if(!is_array($var)){ return null; }
	return count(array_diff_key($var, range(0, count($var) - 1))) == 0;
}

function is_associative_array($var) { return !is_vector_array($var); }

function make_regexp($string,$prepend = null, $append = null, $options = null){
	
	if(is_string($prepend)){
		$string = "/$prepend$string";
	} else {
		$string = "/$string";
	}
	if(is_string($append)){
		$string .= "$append/";
	} else {
		$string .= '/';
	}
	if(is_string($options)){
		$string .= $options;
	}
	return $string;
}

//$filter_words = Array();
// make it a hash or an array, hash will replace, array will remove, or pass in an argument to return true || false

function filter_words($string, $replace = true, $complex_filter = false, $filter_words = null){
	if($filter_words == null){ global $filter_words; } // if the filter_words argument is not passed, assume there is a global
	if($filter_words == null){ die('Please setup the variable $filter_words - It can be either an array or a hash'); } // if there isn't a global, throw a temper
	if(is_vector_array($filter_words)){ $replace = false; } // we can't replace it if it's not a hash
	
	if($replace){
		foreach($filter_words as $key => $value){
			// replace any special chars
			$key = make_regexp($key, null, null, 'i');
			$string = preg_replace($key,$value,$string);
		}
	} else {
		if(is_associative_array($filter_words)){
			// flaten a hash
			$temp = Array();
			foreach($filter_words as $key => $value){
				$temp[] = $key;
			}
			$filter_words = $temp;
		}
		foreach($filter_words as $key => $value){
			// replace any special chars
			$value = make_regexp($value,'\s*',null,'i');
			// ing ed
			$string = preg_replace($value,'',$string);
		}
	}
	return $string;
}


if(isset($_REQUEST['string'])){
	$orginal = $_REQUEST['string'];
} else {
	$orginal = "What the fuck! -->FUCKING<-- SHIT I'll f***ing show you!";
}

if(isset($_REQUEST['filter_with'])){
	$filter        = preg_replace('/\s/', '',$_REQUEST['filter']);
	$filter_array  = split(',',$filter);
	
	$filter_with  = preg_replace('/\s/', '',$_REQUEST['filter_with']);
	$replace_with = split(',',$filter_with);
	
	$filter_words = Array();
	$i = 0;
	foreach($filter_array as $key => $value){
		if(isset($replace_with[$i])){
			$filter_words[$value] = $replace_with[$i];
		} else {
			$filter_words[$value] = '';
		}
		$i ++;
	}
} elseif(isset($_REQUEST['filter'])){
	$filter        = preg_replace('/\s/', '',$_REQUEST['filter']);
	$filter_words  = split(',',$filter);
} else {
	$filter_words = Array();
	$filter_words['fuck(ing|ed|er)?'] = 'f*ck';
	$filter_words['shit'] = 'sh!t';
}

echo '<h2>Results:</h2>';
echo "<strong>Orginal:</strong> $orginal";
echo '<br /><strong>Replaced:</strong> ';
echo filter_words($orginal);
echo '<br /><strong>Removed:</strong> ';
echo filter_words($orginal, false);
?>
<hr />
<h2><?php echo filter_words('Try it using your own fucking settings!') ?></h2>
<h3 style="color:#F00; margin: 0;">PLEASE NOTE:</h3><p style="margin:0;">Your string for the filter setting will be used as regular expression for speed and control, we will not edit it to escape any regular expression marks, please do so yourself. DO NOT worry about the string or replace settings.</p>
<p>The setting below is mainly for removing text, try removing the (ing|ed|er)? and you will see that it works better with the replace, but worse with the remove.</p>
<br />
<form method="post">
	<b>String:</b><font color="#FF0000">*</font> <input name="string" type="text" id="string" value="<?php echo $orginal ?>" size="100" /><br />
    <br />
    <b>Filter:</b><font color="#FF0000">*</font> <input name="filter" type="text" id="filter" value="<?php echo isset($filter) ? $_REQUEST['filter'] : 'fuck(ing|ed|er)?, shit' ?>" size="100" /><br />
    <small>Use comma seperated values. Ex: fuck, you, man</small><br />
    <br />
    <b>Replace:</b><input name="filter_with" type="text" id="filter_with" size="100" value="<?php echo isset($_REQUEST['filter_with']) ? $_REQUEST['filter_with'] : 'f*ck, sh!t' ?>" /><br />
    <small>Use comma seperated values. Ex: f*ck, y0u, hide@address.com</small><br />
    <br />
    <input type="submit" value="test my string!" />
</form>
<hr />
<h2>Todo:</h2>
<ul>
    <li>Filter f*** too, base it off the length and such.</li>
    <li>Lower the ing/ed/etc.</li>
</ul>
<hr />
Title: Simple word filter example<br />
Developed by: <a href='http://www.linkedin.com/in/elabid' target="_blank">Mohammad El-Abid</a> of <a href="http://IsScary.Com" target="_blank">IsScary.Com</a><br />
Developed for: <a href='http://www.webdevrefinery.com/forums/topic/6807-wdc-develop-a-string-replace-system/' target="_blank">wdR comp.</a><br />
Published: 1/21/2011<br />
Return current item: Simple Word Filter