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
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
Share on other sites

I checked that video out Matthew, it's all good until you have to create a replace array.

I have over 500 words, therefore it would be impractical to create an array to replace the 500 words, specifying the swearing format in each word.

 

 

Link to comment
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
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
Share on other sites

 

Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 2644 in ... line 54

 

line 54:

echo preg_replace("/(".implode("|", $words).")/ie", 'censor($0);', $_POST['text']);

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.