Jump to content

Recommended Posts

You could loop through the array passing preg_match an item at a time.

 

foreach($items as $item) {
   preg_match();
}

 

But if your simply checking words and no complex patterns you might be better off using stristr instead of preg_match, depends on the approach your after.

this is how i've layed out my funtion so far:

 

function check_words($str){

 

$bad = array('fuck','shit','ass','cunt','dick','penis','whore','douche','clit','boob','nipple','sex','http://');

if (!preg_match($str,$bad)){

return true;

}else{

return false;

}

 

}

 

i just need a simply way to see if the string contains atleast one of those words, which method do you think is best. I also need to to search for things like "motherfucker" and see "fuck" cause this is a community site.

 

 

Something like..

 

<?php
function check_words($str){

   $bad = array('fuck','shit','ass','cunt','dick','penis','whore','douche','clit','boob','nipple','sex','http://');
   foreach($bad as $word) {
      if (stristr($str, $word)){
            return true;
      }
   }
}
?>

 

... will return true if there is a bad word in the string.

Like cags said, this would loop through and find items with preg_match

 

You may have a bit more flexibility since with preg_match you can tell it to ignore case etc.

 

<?php
$test = array("test", "test2", "matt", "Joe");

function preg_loop($str, $a) {
foreach($a as $k => $v) {
	if(preg_match("/$v/i", $str, $matches)) {
		return "Match";
	}
}
return "No match";
}

echo "Test #1: ".preg_loop("This is just a test string", $test)."<br />"; //Match
echo "Test #2: ".preg_loop("This is just a string", $test)."<br />"; //No match
echo "Test #3: ".preg_loop("This is just a test2 string", $test)."<br />"; //Match
echo "Test #4: ".preg_loop("This is Matt's string", $test)."<br />"; //Match
echo "Test #5: ".preg_loop("This is Joe!", $test)."<br />"; //Match

?>

There is no advantage of using the option you proposed since the example I gave is case insensitive anyway. That's not to say preg_match couldn't allow for more advance filtering. The problem with both the example given by MatthewJ and the one given by myself is you *might* get false possitives. If for example you wish to filter out ass, it will return "This is a class" as containing a banned word. You could try placing spaces either side but then it will not pickup words followed by grammatical characters. If you wish to filter all these cases then preg_match would be the only sensible option, but it won't be the simplest match string if you wish to filter effectively.

There is no advantage of using the option you proposed since the example I gave is case insensitive anyway. That's not to say preg_match couldn't allow for more advance filtering. The problem with both the example given by MatthewJ and the one given by myself is you *might* get false possitives. If for example you wish to filter out ass, it will return "This is a class" as containing a banned word. You could try placing spaces either side but then it will not pickup words followed by grammatical characters. If you wish to filter all these cases then preg_match would be the only sensible option, but it won't be the simplest match string if you wish to filter effectively.

 

You could wrap it in \b instead of \s

 

[pre]

~\b$var\b~

[/pre]

 

 

 

 

\b is shorthand for "word boundary" so it will for instance match

 

" ass "

".ass."

" ass?"

 

but not

 

"class"

 

because \b will fail on that "l" because it is a word character.

 

\b is effectively the opposite of \w and effectively the same as \W

 

 

just a quick note:

 

even though it is effectively the same thing for your purposes, \b is not quite the opposite of \w and not quite the same as \W.  The difference is that \b has zero width assertion, meaning that it will not consume a character in the match, whereas \w and \W will.  Not really relevant for your purposes, but thought I'd clarify that just the same...

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.