php - Check If string contains any of the words -


i need check if string contains 1 of banned words. requirements are:

  1. case insensitive why used stripos()
  2. word should separated spaces, example if banned word "poker", "poker game" or "game poker match" should come under banned string , "trainpokering great" should come under string.

i have tried below

$string  = "poker park great";  if (stripos($string, 'poker||casino') === false)  {  echo "banned words found"; } else  { echo $string; } 

you use array , join it

$arr = array('poker','casino','some','other', 'word', 'regex+++*much/escaping()'); $string = 'cool guy';  for($i = 0, $l = count($arr); $i < $l; $i++) {     $arr[$i] = preg_quote($arr[$i], '/');   // automagically escape regex tokens (think quantifiers +*, [], () delimiters etc...) } //print_r($arr); // check results after escaping  if(preg_match('/\b(?:' . join('|', $arr). ')\b/i', $string)) { // don't need fear      echo 'banned words found'; } else {     echo $string; } 

it uses word boundary , joins array.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -