Jump to content

Find whether something is present in a string


Sickness

Recommended Posts

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

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

<?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.

 

Link to comment
Share on other sites

(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

Link to comment
Share on other sites

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.