Canman2005 Posted October 15, 2007 Share Posted October 15, 2007 Hi all I've done a function to replace swear words. The problem I have is if someone types "class" then it returns "cl***" thinking the last part of "class" (ie: "ass") is a swear word. I had to therefore remove it. Is there a separate replace function I can run just to check for the single word "ass" and then replace it? Thanks in advance Ed Quote Link to comment https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/ Share on other sites More sharing options...
teng84 Posted October 15, 2007 Share Posted October 15, 2007 use regular expression Quote Link to comment https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/#findComment-369576 Share on other sites More sharing options...
hvle Posted October 15, 2007 Share Posted October 15, 2007 You can break the string into an array of words, and match each word (case insensitive) to the list of profanities. Quote Link to comment https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/#findComment-369582 Share on other sites More sharing options...
teng84 Posted October 15, 2007 Share Posted October 15, 2007 You can break the string into an array of words, and match each word (case insensitive) to the list of profanities. long process what if you have say: teng, astig , ako and you have to find them so the process will need loop and if condition right? Quote Link to comment https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/#findComment-369583 Share on other sites More sharing options...
hvle Posted October 15, 2007 Share Posted October 15, 2007 long process what if you have say: teng, astig , ako and you have to find them so the process will need loop and if condition right? Yes it will be a longer code vs the user of regex, but the it will run faster. I see nothing wrong with writing code using loop and condition. Quote Link to comment https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/#findComment-369588 Share on other sites More sharing options...
teng84 Posted October 15, 2007 Share Posted October 15, 2007 i believe with that process of yours regex will run faster Quote Link to comment https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/#findComment-369636 Share on other sites More sharing options...
hvle Posted October 15, 2007 Share Posted October 15, 2007 why don i believe with that process of yours regex will run faster don't believe, you should implement a code and prove regex is faster. Quote Link to comment https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/#findComment-369639 Share on other sites More sharing options...
teng84 Posted October 15, 2007 Share Posted October 15, 2007 im not going to argue do it your way even if takes your coding messy and long ok post your code and help this tread starter with your way Quote Link to comment https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/#findComment-369642 Share on other sites More sharing options...
MadTechie Posted October 15, 2007 Share Posted October 15, 2007 <?php $data = "hello you bad1, how the bad2"; $Bad= array("bad1", "bad2", "bad3"); $Replace = array("***", "***", "***"); $data = str_ireplace($Bad, $Replace, $data); ?> If you want advanced (more flexable) filtering then RegEx would be better, but a little slower RegEx would probably be my choice, as you can catch things which would noramlly require a few lines use any other method simple RegEx Example <?php $data = 'hello you bad1, how the bad2'; $patterns[] = '/bad1/i'; $replacements[] = '***'; $patterns[] = '/bad2/i'; $replacements[] = '***'; $patterns[] = '/f??k/i'; $replacements[] = '***'; $data = preg_replace($patterns, $replacements, $data); ?> You can break the string into an array of words, and match each word (case insensitive) to the list of profanities. That will be more complex and much slower.. Quote Link to comment https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/#findComment-369646 Share on other sites More sharing options...
hvle Posted October 15, 2007 Share Posted October 15, 2007 this is no simple regex, and str_replace. need something that replace whole word, not only patterns. Quote Link to comment https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/#findComment-369650 Share on other sites More sharing options...
MadTechie Posted October 15, 2007 Share Posted October 15, 2007 Yeah see examples posted!!.. its not hard.. infact most would say its kinda simple! Quote Link to comment https://forums.phpfreaks.com/topic/73255-swear-filter-question-class/#findComment-369862 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.