Reflector Posted February 12, 2007 Share Posted February 12, 2007 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! Quote Link to comment Share on other sites More sharing options...
btherl Posted February 12, 2007 Share Posted February 12, 2007 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. Quote Link to comment Share on other sites More sharing options...
Reflector Posted February 12, 2007 Author Share Posted February 12, 2007 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 Do you see another solution for this. Preg_replace is not working. Thanks again! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted February 12, 2007 Share Posted February 12, 2007 ???? 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... Quote Link to comment Share on other sites More sharing options...
Reflector Posted February 13, 2007 Author Share Posted February 13, 2007 Thanks for helping me. It works now and preg-replace did the job. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.