CoreDestroyer Posted October 11, 2012 Share Posted October 11, 2012 I have this textarea, well, registration to be exact. I was wondering if there is a way to filter certain words from getting into the database when signing up. Things such as spaces and inappropriate/bad words. I only have 3 fields at the moment, which are $username $password $email Is there any way to filter those? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/269371-how-to-filter-certain-words-from-textareasdatabase/ Share on other sites More sharing options...
Barand Posted October 11, 2012 Share Posted October 11, 2012 (edited) $badwords = array('microsoft', 'asp', '.net'); $clean = str_replace ($badwords, '', $originalText); Edited October 11, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/269371-how-to-filter-certain-words-from-textareasdatabase/#findComment-1384660 Share on other sites More sharing options...
CoreDestroyer Posted October 11, 2012 Author Share Posted October 11, 2012 $badwords = array('microsoft', 'asp', '.net') $clean = str_replace ($badwords, '', $originalText); I'm guessing I just put that in my code and it'll automatically clean it up? Quote Link to comment https://forums.phpfreaks.com/topic/269371-how-to-filter-certain-words-from-textareasdatabase/#findComment-1384661 Share on other sites More sharing options...
MDCode Posted October 11, 2012 Share Posted October 11, 2012 (edited) str_replace will replace all words associated within $badwords with nothing. If you wish to replace them with certain text, use the second parameter of str_replace and add whatever you want displayed. If you wish to filter words simply edit/add to what barand has shown Edited October 11, 2012 by ExtremeGaming Quote Link to comment https://forums.phpfreaks.com/topic/269371-how-to-filter-certain-words-from-textareasdatabase/#findComment-1384663 Share on other sites More sharing options...
CoreDestroyer Posted October 11, 2012 Author Share Posted October 11, 2012 str_replace will replace all words associated within $badwords with nothing. If you wish to replace them with certain text, use the second parameter of str_replace and add whatever you want displayed. If you wish to filter words simply edit/add to what barand has shown Well thank you very much! One last thing, does this count for spaces? I want to remove that from usernames as well. Quote Link to comment https://forums.phpfreaks.com/topic/269371-how-to-filter-certain-words-from-textareasdatabase/#findComment-1384665 Share on other sites More sharing options...
MDCode Posted October 11, 2012 Share Posted October 11, 2012 I'm not sure on that one as I've never attempted it. You could try a simple space between: --> ' ' <-- as shown. If not idk Quote Link to comment https://forums.phpfreaks.com/topic/269371-how-to-filter-certain-words-from-textareasdatabase/#findComment-1384668 Share on other sites More sharing options...
CoreDestroyer Posted October 11, 2012 Author Share Posted October 11, 2012 I'm not sure on that one as I've never attempted it. You could try a simple space between: --> ' ' <-- as shown. If not idk Ok, thanks anyway! Quote Link to comment https://forums.phpfreaks.com/topic/269371-how-to-filter-certain-words-from-textareasdatabase/#findComment-1384670 Share on other sites More sharing options...
Barand Posted October 11, 2012 Share Posted October 11, 2012 <?php $username = "abc microsoft uses aspx with .net"; $badwords = array('microsoft', 'asp', '.net', ' '); $clean = str_replace ($badwords, '', $username); echo $clean; // --> abcusesxwith ?> Quote Link to comment https://forums.phpfreaks.com/topic/269371-how-to-filter-certain-words-from-textareasdatabase/#findComment-1384671 Share on other sites More sharing options...
Christian F. Posted October 12, 2012 Share Posted October 12, 2012 I would never silently edit user input like this, especially not for things such as usernames, e-mail addresses and such! (Profanity filters is something else.) What you should do, is to use the ctype_* () functions to validate the input according to certain rules. These rules should clearly define what input you expect to be given by the user, and show a warning if anything falls outside of these rules. This is commonly known as "input validation", and there's whole lot of information about this on the net. Some good, but unfortunately a lot of bad stuff too. You should also learn about "output escaping", which is a completely separate concern from input validation, but they're almost always both employed in the same code. Unfortunately that has lead some people to mix them, and treat them as the same. This include people a lot of who have written "security tutorials", which means you might have to search a bit to find some good ones. Quote Link to comment https://forums.phpfreaks.com/topic/269371-how-to-filter-certain-words-from-textareasdatabase/#findComment-1384892 Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2012 Share Posted October 12, 2012 <?php $badwords = array('microsoft', 'asp', '.net', ' '); LOL. Excellent. Quote Link to comment https://forums.phpfreaks.com/topic/269371-how-to-filter-certain-words-from-textareasdatabase/#findComment-1384896 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.