Sickness Posted December 26, 2007 Share Posted December 26, 2007 Hi, i was recently trying to stop all adult phrases from being added into my recent searches by a filter. i figured that i should use strstr or strpos or something and came up with this: $array = array("p**n","s*x","le***n","g*y","a*s","p***y","d***","c**k","f***sh","***g","**x","t***","c***","f**k","p***s"); if(strpos($q, $array) !== false) { stuff to echo if adult word is present } else { stuff to echo if adult words aren't present } $q is the search query $array is all the adult phrases/words and i tried a few other ways that the above too.. but i couldn't get any to work :S can anyone help me on this? would be greatly appreciated thanks, Sickness Quote Link to comment https://forums.phpfreaks.com/topic/83249-find-whether-something-is-present-in-a-string/ Share on other sites More sharing options...
BenInBlack Posted December 26, 2007 Share Posted December 26, 2007 strpos, is not going to iterate thru the array, just make a string, you can leave the commas in $badwordstring = "p**n,s*x,le***n,g*y,a*s,p***y,d***,c**k,f***sh,***g,**x,t***,c***,f**k,p***s"; if(strpos($q, $badwordstring) !== false) { stuff to echo if adult word is present } else { stuff to echo if adult words aren't present } Quote Link to comment https://forums.phpfreaks.com/topic/83249-find-whether-something-is-present-in-a-string/#findComment-423482 Share on other sites More sharing options...
Sickness Posted December 26, 2007 Author Share Posted December 26, 2007 hmm, i dont see how that would work :S ok, ignore my code completely. i need some simple code that will check $q (the search query) for any values from the array $array (with porn words in) so if someone searched "Search test" it would work and if someone searched "porn movies" it wouldn't work can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/83249-find-whether-something-is-present-in-a-string/#findComment-423543 Share on other sites More sharing options...
Sickness Posted December 27, 2007 Author Share Posted December 27, 2007 bump? i really need this possibility of some pay involved! Quote Link to comment https://forums.phpfreaks.com/topic/83249-find-whether-something-is-present-in-a-string/#findComment-423787 Share on other sites More sharing options...
marcus Posted December 27, 2007 Share Posted December 27, 2007 <?php $array = array('porn','sex','whatever','else'); $string = "whatever porn or sex is"; foreach($array AS $bad){ if(preg_match("/$bad/is",$string)){ $error = TRUE; } } if($error){ echo "Your search contained 'sexy' words!"; }else { echo "Your search is confirmed a-okay!"; } ?> tested, it's working. Quote Link to comment https://forums.phpfreaks.com/topic/83249-find-whether-something-is-present-in-a-string/#findComment-423789 Share on other sites More sharing options...
tinker Posted December 27, 2007 Share Posted December 27, 2007 (mgallforever just posted) or You were close! $array = array("p**n","s*x","le***n","g*y","a*s","p***y","d***","c**k","f***sh","***g","**x","t***","c***","f**k","p***s"); foreach($array as $e) { if(strpos($q, $e) !== false) { stuff to echo if adult word is present } else { stuff to echo if adult words aren't present } } P.S. not tested it Quote Link to comment https://forums.phpfreaks.com/topic/83249-find-whether-something-is-present-in-a-string/#findComment-423792 Share on other sites More sharing options...
marcus Posted December 27, 2007 Share Posted December 27, 2007 Tinker, that's going to echo 16 lines because you're working with the foreach statement Quote Link to comment https://forums.phpfreaks.com/topic/83249-find-whether-something-is-present-in-a-string/#findComment-423795 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.