1 Replies - 1745 Views - Last Post: 26 November 2011 - 09:42 PM

#1 f1sh  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 16-October 07

Bad Word Filter

Posted 18 October 2007 - 07:50 AM

Description: Pass the string to be checked to replace()Converts "bad" words into ?#*@!
class swearfilter
{
	static $words = array(
        // TODO: Enter bad words here like so: "bad", 
        );
	
	function replace($haystack)
	{
                // Set the replacements
		$replacements = array("?", "!", "*", "@", "#", "*");
		
                // Loop through each and every word in the $words array
		foreach(swearfilter::$words as $needle)
		{
			$replace = '';
			// Create our replacement string by string length
			for($i=0; $i<strlen($needle); $i++)
			{
				$radnum = rand(0, 5);
				$replace .= $replacements[$radnum];
			}
			
			// Search for the current word and replace
			$haystack = str_ireplace($needle, $replace, $haystack);
		}
                // Return the "clean" text
		return $haystack;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Bad Word Filter

#2 ExpertsGuide  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 26-November 11

Re: Bad Word Filter

Posted 26 November 2011 - 09:42 PM

1. Better keep first (and may be last also) character or the to-be-replaced word. 2. Please put the example also to use it for some HTML form textrea content.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1