chris_rulez001 Posted June 3, 2008 Share Posted June 3, 2008 what is wrong with this? when you post a message with swear words in it all it does is show the swear words. code: $message = $_POST['message']; $R = "<font color=\"FF0000\">*Censored*</font>"; $notallowed = //array removed but all that is here is swear words preg_replace($notallowed, $R, $message); Link to comment https://forums.phpfreaks.com/topic/108524-preg_replace-problems/ Share on other sites More sharing options...
samshel Posted June 3, 2008 Share Posted June 3, 2008 $notallowed = //array removed but all that is here is swear words Is this an array? preg_match takes first parameter as a pattern Link to comment https://forums.phpfreaks.com/topic/108524-preg_replace-problems/#findComment-556438 Share on other sites More sharing options...
chris_rulez001 Posted June 3, 2008 Author Share Posted June 3, 2008 yes that is an array i took the array out because it had swear words in it Link to comment https://forums.phpfreaks.com/topic/108524-preg_replace-problems/#findComment-556439 Share on other sites More sharing options...
samshel Posted June 3, 2008 Share Posted June 3, 2008 Try this: $message = $_POST['message']; $R = "<font color=\"FF0000\">*Censored*</font>"; $notallowed = //array removed but all that is here is swear words for($i=0;$i<count($notallowed);$i++) { $message = preg_replace($notallowed, $R, $message); } Link to comment https://forums.phpfreaks.com/topic/108524-preg_replace-problems/#findComment-556443 Share on other sites More sharing options...
chris_rulez001 Posted June 3, 2008 Author Share Posted June 3, 2008 awsome dude, it works EDIT: thanks Link to comment https://forums.phpfreaks.com/topic/108524-preg_replace-problems/#findComment-556448 Share on other sites More sharing options...
samshel Posted June 3, 2008 Share Posted June 3, 2008 Please mark the post as solved Link to comment https://forums.phpfreaks.com/topic/108524-preg_replace-problems/#findComment-556450 Share on other sites More sharing options...
helraizer Posted June 3, 2008 Share Posted June 3, 2008 At the moment it's just an array, just a block of nothingness that isn't useful. Basically, what I'm trying to say is you need to use foreach to break up the array. <?php $message = $_POST['message']; $R = "<font color='#FF0000'>*Censored*</font>"; $notallowed = array("naughty word", "and another", "another"); foreach($notallowed AS $censor) { if(preg_match("/$censor/i", $message) { $message = preg_replace("/$censor/i", $R, $message); } } ?> Hope that helps, Sam EDIT: either this or Samshel's code'll suffice. Link to comment https://forums.phpfreaks.com/topic/108524-preg_replace-problems/#findComment-556451 Share on other sites More sharing options...
chris_rulez001 Posted June 3, 2008 Author Share Posted June 3, 2008 ok cheers both of you Link to comment https://forums.phpfreaks.com/topic/108524-preg_replace-problems/#findComment-556453 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.