mike12255 Posted October 6, 2009 Share Posted October 6, 2009 is there an away to use preg_match with an array? i want to check a string with an array to see if it contains badwords however i get an error saying string expect it got an array, which i know. Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
mike12255 Posted October 6, 2009 Author Share Posted October 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
Dorky Posted October 6, 2009 Share Posted October 6, 2009 try looking at this http://us2.php.net/in_array is there an away to use preg_match with an array? i want to check a string with an array to see if it contains badwords however i get an error saying string expect it got an array, which i know. Quote Link to comment Share on other sites More sharing options...
mike12255 Posted October 6, 2009 Author Share Posted October 6, 2009 i tried using in array problem is when i use the string "hey fuck" it does not see the word "fuck" i tried expermenting with this would strstr() be good for this in a for each statment? Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
mike12255 Posted October 6, 2009 Author Share Posted October 6, 2009 ok thanks cags ill try it when i get home, off to school now. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted October 6, 2009 Share Posted October 6, 2009 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 ?> Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
mike12255 Posted October 6, 2009 Author Share Posted October 6, 2009 is it possible to check for a swear word, then got a character length to check if the character length matches the length of the swear word? Quote Link to comment Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 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] Quote Link to comment Share on other sites More sharing options...
mike12255 Posted October 6, 2009 Author Share Posted October 6, 2009 crayon, what would that do? Quote Link to comment Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 \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 Quote Link to comment Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 Ahh I didn't know about \b, so your regex would simply look like... "~\b$word\b~i" Quote Link to comment Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 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... 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.