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

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

}

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?

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

 

(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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.