php - Check If string contains any of the words -
i need check if string contains 1 of banned words. requirements are:
- case insensitive why used
stripos()
- 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
Post a Comment