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
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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

????

 

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.