6

I need to check if a string contains any one of the banned words. My requirements are:

  1. Case insensitive which is why I used stripos()
  2. Word should be separated by spaces, for example if banned word is "poker", "poker game" or "game poker match" should come under banned string and "trainpokering great" should come under good string.

I have tried something like below

$string  = "poker park is great";

if (stripos($string, 'poker||casino') === false) 
{

echo "banned words found";
}
else

{
echo $string;
}
2
  • 4 answers ! nothing works as expected , Any other answers are welcome Commented Dec 16, 2014 at 8:53
  • Sorry, my solution (if (preg_match("/\b(poker|casino)\b/i", $string)) ) just works for all your use cases... :-) For example, "poker", "poker game" or "game poker match" are marked as banned, and "trainpokering great" is marked as good... Why do you say it doesn't?
    – MarcoS
    Commented Dec 16, 2014 at 13:38

4 Answers 4

7

Use preg_match to match a regular expression:

$string  = "poker park is great";

if (preg_match("/(poker|casino)/", $string)) {
  echo "banned words found";
} else {
  echo $string;
}

UPDATE: As suggested in comments and in Mayur Relekar answer, if you want your match to be case-insensitive, you should add an i flag to your regex.
And, if you want to match words (i.e., "poker" should be preceded and followed by a word-boundary, for example a space, a punctuation, or an end-of-file), you should enclose your matching group with \b...
So:

...
if (preg_match("/\b(poker|casino)\b/i", $string)) {
...
1
  • why does this get +6 ? This is case sensitive ,, -1 Commented Dec 16, 2014 at 8:49
1

MarcoS is right, except that in your case you need to match the exact string and not an unbound string. For this, you need to prefix and suffix \b (\b is a word delimiter) to the string you want to exactly match.

$string  = "poker park is great";
if (preg_match("/\bpoker\b/", $string)) {
    echo "banned words found";
} else {
    echo $string;
}
1
  • It's working fine but as @DevakiArulmami wants it working for multiple banned words so I just modified the code.
    – Neo
    Commented Dec 16, 2014 at 9:58
1
$string  = "park doing great with pokering. casino is too dangerous.";
$needles = array("poker","casino");
foreach($needles as $needle){
  if (preg_match("/\b".$needle."\b/", $string)) {
    echo "banned words found";
    die;
  }
}
echo $string;
die;
3
  • when string is Pokering , It still says its banned Commented Dec 16, 2014 at 8:52
  • I think now you have solution of your problem. By adding banned words to the declared array $needles you can check it for multiple words also.
    – Neo
    Commented Dec 16, 2014 at 10:06
  • 1
    modification for case insensitive matches ... if (preg_match("/\b".$needle."\b/i", $string)) ...
    – Da Beginer
    Commented Aug 11, 2020 at 16:09
0

You could use an array and 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 about quantifiers +*, [], () delimiters etc...)
}
//print_r($arr); // Check the results after escaping

if(preg_match('/\b(?:' . join('|', $arr). ')\b/i', $string)) { // now we don't need to fear 
    echo 'banned words found';
} else {
    echo $string;
}

It uses word boundary and joins the array.

7
  • how to implement multiple words in this ? Commented Dec 16, 2014 at 8:50
  • sorry but this code will no longer work for the string like "dangerous game is poker." @DevakiArulmami
    – Neo
    Commented Dec 16, 2014 at 9:42
  • PO didn't speak about speed constraints... If that is not a specific request, I'd avoid using "tricks" (adding spaces to the string)... What if words are preceded/followed by punctuation chars? This solution looks quite 'restrictive', at least... :-)
    – MarcoS
    Commented Dec 16, 2014 at 11:42
  • @DevakiArulmami now it does what you want it to, I suppose.
    – Amit Joki
    Commented Dec 16, 2014 at 11:49
  • @AmitJoki I would also use preg_quote().
    – HamZa
    Commented Dec 16, 2014 at 12:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.