dadamssg87 Posted April 6, 2011 Share Posted April 6, 2011 I can't seem to find a decent code function to search for "curse" words that i would deem inappropriate in an array. Count the number of instances the function finds a curse word and then if the number is greater than one halt the script. something like the following but not really.... $words = array('ass','fuck','shit'); $string = 'Blah blah blah ass blah blah fuck blah blah shit!'; //code to count the instances of the curse words(3) if($instances > 0) { echo "no cursing please."; } ; any help on how to do this would be much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/ Share on other sites More sharing options...
3raser Posted April 6, 2011 Share Posted April 6, 2011 EDIT Sorry, fixed it. <?php $words = array('ass','fuck','shit'); $string = 'Blah blah blah ass blah blah fuck blah blah shit!'; $validate = explode(" ", $string); if(in_array($words)) { echo "No cursing please."; } else { //no cussing found } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1197901 Share on other sites More sharing options...
spiderwell Posted April 6, 2011 Share Posted April 6, 2011 all that does is check the bad words array , not the string. Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1197906 Share on other sites More sharing options...
3raser Posted April 6, 2011 Share Posted April 6, 2011 all that does is check the bad words array , not the string. Just fixed it as you posted. Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1197908 Share on other sites More sharing options...
spiderwell Posted April 6, 2011 Share Posted April 6, 2011 hehe cool, i would offer up a solution but am actually trying to work on a project. Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1197913 Share on other sites More sharing options...
dadamssg87 Posted April 6, 2011 Author Share Posted April 6, 2011 awesome, thanks fellas! Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1197917 Share on other sites More sharing options...
spiderwell Posted April 6, 2011 Share Posted April 6, 2011 try this mate, no offence to other poster, but I can't see that snippet working, or maybe I am just going mad: <?php $i = 0; $words = array('ass','fuck','shit'); $string = 'Blah blah blah ass blah blah fuck blah blah shit!'; foreach ($words as $value) { if (strpos($string,$value)) { $i ++; } if($i>3) echo "naughty words limit"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1197924 Share on other sites More sharing options...
spiderwell Posted April 6, 2011 Share Posted April 6, 2011 actually that is flawed too, give me a few more mins, haha Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1197925 Share on other sites More sharing options...
spiderwell Posted April 6, 2011 Share Posted April 6, 2011 this should do the trick, the mistake i made was with instr() it didnt count number of times a rude word might appear, so was looking for 3 different rude words, rather than a total of 3 rude words, be they all different or the same. EDIT*** i should be shot for skim reading, this allows you to have 2 rude words and not more, really as soon as you get a hit of 1 curse word you want it to stop. i will post YET A?NOTHER solution haha i think i need to go to bed!! ****** <?php $i = 0; $limit = false; $words = array('ass','fuck','shit'); $string = 'ass ass blah blah ass blah blah shit blah ass!'; foreach ($words as $value) { $i += substr_count($string,$value);// includes multiple counts of the same naughty word if($i>=3) { $limit = true; echo "naughty word limit"; } if ($limit)break;// stops the search going any further } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1197933 Share on other sites More sharing options...
spiderwell Posted April 6, 2011 Share Posted April 6, 2011 3rd time lucky (god i hope so!) <?php $words = array('ass','fuck','shit'); $string = 'Blah blah blah ass blah blah fuck blah blah shit!'; foreach ($words as $value) { if (strpos($string,$value)) { echo "naughty words limit"; break; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1197936 Share on other sites More sharing options...
3raser Posted April 7, 2011 Share Posted April 7, 2011 Didn't see post above. I recommend using his way, less code. Tested, and works. <?php //bad words 0 1 2 $words = array('asshhfghh', 'gfhfghfgh', 'fuck'); //string to search $string = 'Blah blah blah ass blah blah fuck blah blah shit!'; //amount in the array $x = count($words) + 1; while($i < $x) { if(strpos($string, $words[$i]) === false) { ++$i; } else { //holds the number of bad words found ++$b; ++$i; } } if(!$b) { echo "No bad words found, yippe!"; } else { echo "Bad words found."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1197992 Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 yeah what confused me most was that I thought a count was necessary, when really its a sort of boolean search, there either is or isnt a curse word, one instance will make it true. thats why my final snippet was much shorter, as no counting was involved Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1198011 Share on other sites More sharing options...
dadamssg87 Posted April 7, 2011 Author Share Posted April 7, 2011 hmm..i tried to implement this code <?php $words = array( 'fuck','shit', 'dick', 'cunt', 'twat', 'motherfucker', 'asshole', 'damn', 'bitch', 'bitching', 'fucking', 'fuckin', 'shitting', 'shittin', 'whore', 'whores', 'slut', 'sluts', 'bullshit', 'ass', 'bitches'); //string to search $string = $sentence; //amount in the array $x = count($words) + 1; while($i < $x) { if(strpos($string, $words[$i]) === false) { ++$i; } else { //holds the number of bad words found ++$b; ++$i; } } if(!$b) { //echo "No bad words found, yippe!"; } else { $_SESSION['message'] = "Your sentence contains curse words. Please remove the foul language."; die(header("location: test.php")); } but it doesn't count them as individual words. For example "I want to go bass fishing" will throw an error for the word "bass" because "ass" is in it. Any ideas how to make it only search for words? It doesn't seem to work if i just put spaces around the words in the array. Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1198038 Share on other sites More sharing options...
3raser Posted April 7, 2011 Share Posted April 7, 2011 <?php $words = array( 'fuck','shit', 'dick', 'cunt', 'twat', 'motherfucker', 'asshole', 'damn', 'bitch', 'bitching', 'fucking', 'fuckin', 'shitting', 'shittin', 'whore', 'whores', 'slut', 'sluts', 'bullshit', 'ass', 'bitches'); //string to search $string = $sentence; //amount in the array $x = count($words); for($i = 0; $i < $x; ++$i) { if(strpos($string, $words[$i]) === false) { ++$i; } else { //holds the number of bad words found ++$b; ++$i; } } if(!$b) { //echo "No bad words found, yippe!"; } else { $_SESSION['message'] = "Your sentence contains curse words. Please remove the foul language."; die(header("location: test.php")); } ?> This will count them as individual words. Quote Link to comment https://forums.phpfreaks.com/topic/232908-counting-certain-words-in-a-string/#findComment-1198045 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.