Jump to content

Bad Words


anevins

Recommended Posts

: : NEW Problem

 

I want to replace bad words which someone has inputted into my HTML textarea, with something like [CENSORED].

Here's what I've got so far:

note: $review is the posted textarea name.

Code: [select]

 

$words_text = 'badwords.txt';

$bad_words = file($words_text);

 

$good_words = str_ireplace($bad_words,'[CENSORED]',$review);

 

echo $good_words;

 

 

The thing is, if I type bad words in the textarea, I still get bad words when I echo $good_words.

 

I don't know why I'm getting this, any ideas

Link to comment
https://forums.phpfreaks.com/topic/232523-bad-words/
Share on other sites

Yeah, if I var_dump $bad_words, I get the file contents.

 

This is the example I'm trying to follow (this code works)

$words = array('apple','orange','banana');
$string = 'How was your apple? I like oranges! But bananas are even better!';
$fixed = str_ireplace($words,'(SNIP)',$string);
echo "String: $string<br><br>Fixed: $fixed<br>"; 

Link to comment
https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196039
Share on other sites

I also made a bad words filter to do single words or entire posts.

 

The demo is there, able to download, can also see the code below the demo.

 

http://get.blogdns.com/dynaindex/Safe-Filter/

 

One day I'll have to make this a function, but was never at the top of my list.

Link to comment
https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196044
Share on other sites

I think that's the same as Matthew's example.

I can't use those replacing arrays as it requires me to specify what the new format of each word will be, as I have too many words.

 

I'm using the file() function to load in the text file with my words, into an array called $bad_words

Link to comment
https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196053
Share on other sites

assuming your file has one word per line:

 

<?php
$file = 'myfile.txt';
$handle = fopen($file, 'rb');
$contents = fread($handle, filesize($file));
fclose($handle);

$words = explode("\n", $contents);

function censor($string){
    $total = strlen($string);
    return str_repeat("*", $total);
}
echo preg_replace("/(".implode("|", $words).")/ie", 'censor($0);', $_POST['text']);
?>

Link to comment
https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196056
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.