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?? 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. 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 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 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 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 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
Archived
This topic is now archived and is closed to further replies.