matfish Posted June 24, 2007 Share Posted June 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 24, 2007 Share Posted June 24, 2007 just use str_replace ~ Chocopi Quote Link to comment Share on other sites More sharing options...
matfish Posted June 24, 2007 Author Share Posted June 24, 2007 Yeah, Im that far ahead - but you think its best to run an array thru all the words? Quote Link to comment Share on other sites More sharing options...
matfish Posted June 24, 2007 Author Share Posted June 24, 2007 I wanted to do something like if "balls" then "b***s" kinda thing but with a list of bad language words... Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 24, 2007 Share Posted June 24, 2007 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 ~ Chocopi Quote Link to comment Share on other sites More sharing options...
matfish Posted June 24, 2007 Author Share Posted June 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
chigley Posted June 24, 2007 Share Posted June 24, 2007 Use str_ireplace() instead of str_replace() Quote Link to comment Share on other sites More sharing options...
matfish Posted June 24, 2007 Author Share Posted June 24, 2007 Hi, str_ireplace throw's a wobbly. Call to undefined function. Also chocopi - it puts a line break after the word so after every bad word there is a new line... Thanks for everyones help Quote Link to comment Share on other sites More sharing options...
Zane Posted June 24, 2007 Share Posted June 24, 2007 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 Quote Link to comment Share on other sites More sharing options...
matfish Posted June 24, 2007 Author Share Posted June 24, 2007 Hey thanks for the reply, but it dont work. Text is just output the same ? Quote Link to comment Share on other sites More sharing options...
matfish Posted June 24, 2007 Author Share Posted June 24, 2007 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? Quote Link to comment Share on other sites More sharing options...
rcorlew Posted June 25, 2007 Share Posted June 25, 2007 The reason str_ireplace throws a wobbly is because that function is only available in versions > 5. I have a custom function that replaces str_ireplace for versions < 5 if you would like. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.