daveh33 Posted December 27, 2007 Share Posted December 27, 2007 Trying to use a simple filter $bad_words = explode(',', 'badword1, badword2, badword3'); foreach ($bad_words as $naughty) { $message = eregi_replace($naughty, "#!@%*#", $message); } This seems to filter for badword1 only - any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/83395-solved-php-word-filter-eregi_replace/ Share on other sites More sharing options...
GingerRobot Posted December 27, 2007 Share Posted December 27, 2007 Well, i assume that your message contains a string with no spaces. If all the words had spaces between, it would work. To make it work, remove the spaces in the string that is the 2nd parameter of the explode function: $bad_words = explode(',', 'badword1,badword2,badword3'); A more efficient implementation would be: <?php <?php $black_list = array('badword1','badword2','badword3'); str_replace($blacklist,'#!@%*#',$message); ?> Since it avoids the overhead of the regular expression. Quote Link to comment https://forums.phpfreaks.com/topic/83395-solved-php-word-filter-eregi_replace/#findComment-424295 Share on other sites More sharing options...
daveh33 Posted December 27, 2007 Author Share Posted December 27, 2007 OK I tried:- <?php $black_list = array('badword1','badword2','badword3'); str_replace($blacklist,'#!@%*#',$message); ?> It doesnt seem to change the text at all when i echo $message Quote Link to comment https://forums.phpfreaks.com/topic/83395-solved-php-word-filter-eregi_replace/#findComment-424296 Share on other sites More sharing options...
MadTechie Posted December 27, 2007 Share Posted December 27, 2007 <?php $message = "badword1 test badword2"; $black_list = array('badword1','badword2','badword3'); $message = str_replace($black_list,'#!@%*#',$message); echo $message; ?> $black_list not $blacklist also you need to return the result from str_replace Quote Link to comment https://forums.phpfreaks.com/topic/83395-solved-php-word-filter-eregi_replace/#findComment-424298 Share on other sites More sharing options...
GingerRobot Posted December 27, 2007 Share Posted December 27, 2007 Whoops, sorry - i'm tired Quote Link to comment https://forums.phpfreaks.com/topic/83395-solved-php-word-filter-eregi_replace/#findComment-424302 Share on other sites More sharing options...
daveh33 Posted December 27, 2007 Author Share Posted December 27, 2007 Great thats working - thanks for your F1 Quote Link to comment https://forums.phpfreaks.com/topic/83395-solved-php-word-filter-eregi_replace/#findComment-424305 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.