Jump to content

Recommended Posts

Hey all, sorry if this has been posted and explained before. I've searched several times, but I'm just not really even sure what to search for.  ;D I'm basically trying to define an array of "bad words" and I want to check string1 and make sure that none of the items in the array are found to occur in string1. I basically want to throw out any string1's that contain any "bad words". By bad words, I mainly mean that I'm trying to filter common words used in bot messages. So I basically don't know what to use in php to check in the string for these bad words. I'm pretty sure that I'll have to use a loop of some sort (as there are no built in functions to my knowledge that automate this type of search). I searched on the php manual site and was unsure of which function to use. I was thinking maybe use "strstr" and on the first occurrence, just throw out the message all together. But I want to make sure that this is a speedy/accurate method of doing what my goal is. I also know that I will need to make the comparisons case insensitive...and I found kind of what I'm looking for here, but am unable to really interpret it completely (still a n00b, lol).. http://fundisom.com/phpsnippets/snip/string_handling/string_in_string/ Thanks all in advanced.

Here's an example of replacing any "bad words" with *s

 

<?php
$string = 'My favorite ice cream is chocolate';
$badWords = array('cream', 'chocolate'); // Of course you would do different words.

foreach($badWords as $word)
{
$length = strlen($word);
$replacement = str_irepeat('*', $length);

$string = str_replace($word, $replacement, $string);
}

echo $string; // "My favorite ice ***** is *********"
?>

 

or to just remove the words...

 

<?php
$string = 'My favorite ice cream is chocolate';
$badWords = array('cream', 'chocolate'); // Of course you would do different words.

foreach($badWords as $word)
{
$string = str_ireplace($word, '', $string);
}

echo $string; // "My favorite ice  is 
?>

 

To be able to throw out the message completely I guess you could do this...

 

<?php
$string = 'My favorite ice cream is chocolate';
$badWords = array('cream', 'chocolate'); // Of course you would do different words.

foreach($badWords as $word)
{
if(stristr($string, $word) != false)
{
	$error = true;
	break;
}
}

if($error == true)
{
echo 'You used a bad word!';
}
else {
echo $string;
}
?>

@genericnumber1

Thanks! I'll give it a shot with throwing them out completely. I don't really care about filtering language on my site cause I'm not worried about foul language, I just have bots posting about meds and other stuff, etc. I just wanna throw out the bot posts. No one else on my site is gonna be posting about viagra, lol. Thanks for the fast response!

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.