Jump to content

preg_replace problems


chris_rulez001

Recommended Posts

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

At the moment it's just an array, just a block of nothingness that isn't useful. :P

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.