Jump to content

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

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.