anevins Posted April 2, 2011 Share Posted April 2, 2011 : : 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 Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/ Share on other sites More sharing options...
matthew9090 Posted April 2, 2011 Share Posted April 2, 2011 change $bad_words = file($words_text); to $bad_words = file_get_contents($words_text); i think Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196031 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 fopen requires 2 parameters Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196033 Share on other sites More sharing options...
matthew9090 Posted April 2, 2011 Share Posted April 2, 2011 i meant file_get_contents Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196035 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 same result as first post. Note: The variable $bad_words does have the value of the text file. Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196037 Share on other sites More sharing options...
Pikachu2000 Posted April 2, 2011 Share Posted April 2, 2011 Is the text file in the same directory as the script that's calling the file() function? Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196038 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196039 Share on other sites More sharing options...
matthew9090 Posted April 2, 2011 Share Posted April 2, 2011 there is a tutorial about this: http://www.youtube.com/watch?v=5y1HyQLg2dU&list=PLFA954987101252EF&index=25 Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196040 Share on other sites More sharing options...
QuickOldCar Posted April 2, 2011 Share Posted April 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196044 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 That looks really complicated to me, I don't think I will be able to understand it just for a simple form textarea box Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196048 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196050 Share on other sites More sharing options...
The Little Guy Posted April 2, 2011 Share Posted April 2, 2011 http://phpsnips.com/snippet.php?id=42 Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196051 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196053 Share on other sites More sharing options...
The Little Guy Posted April 2, 2011 Share Posted April 2, 2011 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']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196056 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 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']); Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196059 Share on other sites More sharing options...
The Little Guy Posted April 2, 2011 Share Posted April 2, 2011 does your file have an extra new line with nothing there? if so remove it, I think I got the same error because of that. Make sure there are no empty lines in the file. Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196060 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 All lines are accounted for, there are no empty lines Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196063 Share on other sites More sharing options...
The Little Guy Posted April 2, 2011 Share Posted April 2, 2011 try changing this line: $contents = fread($handle, filesize($file)); To this: $contents = preg_quote(fread($handle, filesize($file))); Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196064 Share on other sites More sharing options...
.josh Posted April 2, 2011 Share Posted April 2, 2011 another filter... http://www.phpfreaks.com/forums/index.php?topic=228009.0 Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196065 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 Reply to the little guy, Nothing is returned now. Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196067 Share on other sites More sharing options...
The Little Guy Posted April 3, 2011 Share Posted April 3, 2011 try changing this line: $words = explode("\n", $contents); To this: $words = explode("\r\n", $contents); and btw, here is an example of a working copy: http://www.weblyize.com/test.php?text=damn+stupid+fucker Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196087 Share on other sites More sharing options...
matthew9090 Posted April 4, 2011 Share Posted April 4, 2011 if your text file has 1 per line then you need to read it line by line and somehow put each line into an array which if your using that tutorial i gave u. Quote Link to comment https://forums.phpfreaks.com/topic/232523-bad-words/#findComment-1196558 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.