Jump to content

[SOLVED] Help - form input TEXTarea filter!


Reflector

Recommended Posts

Hey guys,

 

I have been working on this problem for days with no success.

I've created a maiform page and I want to filter out badwords from the TEXT AREA.

I'm new to PHP Here's what I want to do.

 

$badword = array ("bad1" , "bad2" , "bad3" , "bad4");

$replacebadword "bad**";

 

foreach($badword as $bad) {

 

$message = preg_replace( $bad, $replacebadword, $message);

}

   

 

print "$message";

 

My intention was to replace the badword in my mailform textarea.

I'm usung foreach to loop threw the badword array to see if they can be found in the text string - $message.

The code here will not replace at all.

 

Any help would be greatly appreciated!

 

 

Link to comment
https://forums.phpfreaks.com/topic/38135-solved-help-form-input-textarea-filter/
Share on other sites

preg_replace uses perl regular expressions, which have a special syntax.  For your situation, you should use str_replace() instead, since you don't need the power of regular expressions.

 

Just $message = str_replace($bad, $replacebadword, $message);

 

Note that that will also remove bad words that are parts of words, like the town of Scunthorpe.

Thank you for helping and it was working too. But after testing I got som funny messages as part of the words was replaced. I was thinking of making a bigger array of badwords. This solution wil introduce too many odd words ;D

Do you see another solution for this. Preg_replace is not working.

 

Thanks again!

????

 

Preg_replace is the perfect tool for the job in this case and you don't need a loop!

 

Just this,

 

<?php
$badword = array ('/fl(i|a)p/i' , '/t(a|o)t/i' , '/ira(q|n)/i');

$message = preg_replace( $badword, '***', $message);
?>

 

by making an array of regular_expressions you can simplfy the job...

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.