anevins Posted April 2, 2011 Share Posted April 2, 2011 Hi there, I have a HUGE list of inappropriate words in plain text, but the thing is, I want to use these bad words in an array. But I can't just copy and paste the bad words into the array, as each bad word needs to have two apostraphes aside one another. E.g the array should be: $bad_words = array('badword1','badword2','badword3'); but my text document just has: badword1 badword2 badword3 How do I format the bad words to have apostraphes aside each other? I don't want to manually go through each word. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/232517-how-to-format-loads-of-words/ Share on other sites More sharing options...
Pikachu2000 Posted April 2, 2011 Share Posted April 2, 2011 If they're all in a file withe line breaks between them, use file() to read them into an array. Quote Link to comment https://forums.phpfreaks.com/topic/232517-how-to-format-loads-of-words/#findComment-1196000 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 Oops, looks like I need to format even more as each word needs a comma after it aswell as the apostrophes Quote Link to comment https://forums.phpfreaks.com/topic/232517-how-to-format-loads-of-words/#findComment-1196001 Share on other sites More sharing options...
nethnet Posted April 2, 2011 Share Posted April 2, 2011 If you have them in a text file, with each word on a separate line, you could just do this to create your array: <?php $file = "/path/to/file/bad_words.txt"; $handle = fopen($file, "r"); $contents = fread($handle, filesize($file)); $bad_words = explode("\n", $contents); ?> Quote Link to comment https://forums.phpfreaks.com/topic/232517-how-to-format-loads-of-words/#findComment-1196002 Share on other sites More sharing options...
nethnet Posted April 2, 2011 Share Posted April 2, 2011 Use Pikachu's method, file() completely slipped my memory.. wraps what I posted into one nice little function. Quote Link to comment https://forums.phpfreaks.com/topic/232517-how-to-format-loads-of-words/#findComment-1196003 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author Share Posted April 2, 2011 Thanks guys, this problem has been solved through the file() function. Hooray Quote Link to comment https://forums.phpfreaks.com/topic/232517-how-to-format-loads-of-words/#findComment-1196004 Share on other sites More sharing options...
anevins Posted April 2, 2011 Author 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. $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/232517-how-to-format-loads-of-words/#findComment-1196018 Share on other sites More sharing options...
sasa Posted April 3, 2011 Share Posted April 3, 2011 try to trim array elements before use it <?php $words_text = 'badwords.txt'; $bad_words = file($words_text); foreach($bad_words as $k => $v) $bad_words[$k] = trim($v); $good_words = str_ireplace($bad_words,'[CENSORED]',$review); echo $good_words; ?> Quote Link to comment https://forums.phpfreaks.com/topic/232517-how-to-format-loads-of-words/#findComment-1196139 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.