Jump to content

Array word filter?


matfish

Recommended Posts

Hey,

 

got a comment/blog type thingy and in the users preferences they have the option to turn a bad language filter on/off.

 

Im just wondering the quickest way to implement this to view the output? You think put the words into an array and if the word filter is true, then how would i go through all the array of bad words and replace with *** and would this be quick or would it delay viewing the output?

 

Many thanks

Link to comment
Share on other sites

well I would use something like this:

 

<?php

$text = "I like balls with poop and a bit of bugger";

$lines = file("naughty_words.txt");

foreach($lines as $line)
{
list($find, $replace) = explode("=", $line);
$text = str_replace($find, $replace, $text);
}

?>

 

then in naughty_words.txt just store the info like

 

balls=b***s

poop=p**p

bugger=b****r

 

this should then give:

 

I like b***s with p**p and a bit of b****r

 

Hope that helps ;D

 

~ Chocopi

Link to comment
Share on other sites

Hey, thanks - seems to work fine and doesn't interfere with the speed of display (which is what i was worried about)

 

seems to work only with words that match (case sensitive) - meaning: buGGer is not the same as bugger which is in the list.. any ideas?

 

Thanks again

 

Link to comment
Share on other sites

you need a function that censors words first

--note this function passes by reference

function censorWord($word) {
  for($i=0, $i      if($i != 0 || $i != strlen($word)++)
        $word{$i} = "*";
  }
  return $word;
}

 

then do the same idea as before

$text = "I like balls with poop and a bit of bugger";

$lines = file("naughty_words.txt");

foreach($lines as $line) {
$text = str_replace($line, censorWord($line), $text);
}

 

but in your naugty.txt file just name all the words down the line you won't need the equals marks and such

just

 

balls

poop

bugger

etc

etc

etc

 

Link to comment
Share on other sites

function BadWordFilter($text)
{
$bads = array (
	array("ass","a*s"),
	array("piss","p**s"),
	array("crap","c**p")
);

	for($i=0;$i<sizeof($bads);$i++) {
		$text = eregi_replace($bads[$i][0],$bads[$i][1],$text);
	}
	return $text;
}

 

The above woks ok, just wondering if reading from the txt file is quicker or via an array?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.