kayz100 Posted September 15, 2013 Share Posted September 15, 2013 PHPfreaks your script woks fine. However I would like to make a few modifications and apply it inside input fields, how do I do that? Lets say my input fields are as foolows: <input type='text' name='badwords' /> <!-- where do I put php on this input field as a php include file--> <?php $badwords = array('bad1', 'bad2', 'bad3', 'ass');$text = 'This is a test. Ass. Grass. bad1.';function filterBadwords($text, array $badwords, $replaceChar = '*') { return preg_replace_callback( array_map(function($w) { return '/\b' . preg_quote($w, '/') . '\b/i'; }, $badwords), function($match) use ($replaceChar) { return str_repeat($replaceChar, strlen($match[0])); }, $text );}echo filterBadwords($text, $badwords); ?> Quote Link to comment https://forums.phpfreaks.com/topic/282159-bad-words-filtering/ Share on other sites More sharing options...
jcbones Posted September 15, 2013 Share Posted September 15, 2013 Are you talking about adding bad words to a bad words list? Not tested- May not be error free <?php if(!empty($_POST['badwords'])) { if(strpos(',',$_POST['badwords']) !== false) { $arr = explode(',',$_POST['badwords']); $str = implode(PHP_EOL,$arr); } else { $str = $_POST['badwords'] . PHP_EOL; } file_put_contents('badwords.txt',$str,FILE_APPEND); } if(file_exists('badwords.txt')) { $badwords = file('badwords.txt'); } else { $badwords = array(); } $text = 'This is a test. Ass. Grass. bad1.'; function filterBadwords($text, array $badwords, $replaceChar = '*') { return preg_replace_callback( array_map(function($w) { return '/\b' . preg_quote($w, '/') . '\b/i'; }, $badwords), function($match) use ($replaceChar) { return str_repeat($replaceChar, strlen($match[0])); }, $text ); } echo filterBadwords($text, $badwords); ?> <form action="" method="post"> <label for="badwords">Add a Word to the Filter</label><br /> <input type="text" name="badwords" /> <input type="submit" name="submit" value=" ADD " /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/282159-bad-words-filtering/#findComment-1449543 Share on other sites More sharing options...
kayz100 Posted September 16, 2013 Author Share Posted September 16, 2013 Hi guru I meant how go I get users input to replace any badwords with symbols? My biggest problem is using input field with PHP, as a newbie I am struggling a bit so any help would be welcomed and appreciated greatly. I mean the above example for instance the badwords.text am i correct to add bad words in the file as follows: "bad1","bad2","bad3" or do I have to enter badwords this way: "bad1, bad2, bad3" I want to make sure that when user type in a bad word in the field it alerts them that thier badword will be replaced with *** Thank you once again Quote Link to comment https://forums.phpfreaks.com/topic/282159-bad-words-filtering/#findComment-1449713 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.