cordoprod Posted March 24, 2008 Share Posted March 24, 2008 Hi. I need some help with a bad word filter. I have a variable to store the words in, seperated by comma. Like this: $badwords = "bad1,bad2,bad3,bad4"; Then I can use the explode(",", $badwords); But, I now i don't know what to do. I want to cut the away all letters but one, and replacing those letters with ** and keep the length of the word. Like bad1 would be like this: b*** Anybody here that can help me? Quote Link to comment https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/ Share on other sites More sharing options...
papaface Posted March 24, 2008 Share Posted March 24, 2008 Something like: <?php $badwords = array("bad1","bad2","bad3"); $post = "this is a bad1 word."; foreach ($badwords as $key => $word) { $len = strlen($word) -1; while ($len > 0) { $asterix .= "*"; $len--; } $post = str_ireplace($word,$word{0} . $asterix,$post); } echo $post; ?> Untested. Quote Link to comment https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499516 Share on other sites More sharing options...
rhodesa Posted March 24, 2008 Share Posted March 24, 2008 same concept...a little shorter: <?php $badwords = array("bad1","bad2","bad3"); $post = "this is a bad1 word."; foreach ($badwords as $key => $word) $post = str_ireplace($word,$word{0}.str_repeat('*',strlen($word)-1),$post); echo $post; ?> Quote Link to comment https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499525 Share on other sites More sharing options...
papaface Posted March 24, 2008 Share Posted March 24, 2008 same concept...a little shorter: <?php $badwords = array("bad1","bad2","bad3"); $post = "this is a bad1 word."; foreach ($badwords as $key => $word) $post = str_ireplace($word,$word{0}.str_repeat('*',strlen($word)-1),$post); echo $post; ?> ooo I didn't know there was a str_repeat function. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499529 Share on other sites More sharing options...
cordoprod Posted March 24, 2008 Author Share Posted March 24, 2008 When i use the code i get this error: Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 16252905 bytes) in /customers/cordoproduction.com/cordoproduction.com/httpd.www/php/collection/Guestbook/guestbook.php on line 383 Quote Link to comment https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499534 Share on other sites More sharing options...
papaface Posted March 24, 2008 Share Posted March 24, 2008 When i use the code i get this error: Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 16252905 bytes) in /customers/cordoproduction.com/cordoproduction.com/httpd.www/php/collection/Guestbook/guestbook.php on line 383 Which code are you talking about? The code in my box works, I made a change to it. You must have used the code a second before I corrected it. Quote Link to comment https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499536 Share on other sites More sharing options...
MadTechie Posted March 24, 2008 Share Posted March 24, 2008 Keep it simple <?php $badwords = array("bad1","bad2","bad3"); $post = "this is a bad1 word."; $post = str_ireplace($badwords,"****",$post); echo $post; ?> Quote Link to comment https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499537 Share on other sites More sharing options...
cordoprod Posted March 24, 2008 Author Share Posted March 24, 2008 The first one. Does it have anything to do with I am having a lot of bad words in an array? Quote Link to comment https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499540 Share on other sites More sharing options...
MadTechie Posted March 24, 2008 Share Posted March 24, 2008 yes that would affect the memory requirements, but the loop isn't needed, try my post its short and simple, Quote Link to comment https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499543 Share on other sites More sharing options...
cordoprod Posted March 24, 2008 Author Share Posted March 24, 2008 It worked fine now Thanks to all of you ! Quote Link to comment https://forums.phpfreaks.com/topic/97626-bad-words-filter-help/#findComment-499546 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.