preg_match array items in string?
algorithm, php, preg-match, string-parsing
Solution
How about this:
$badWords = array('one', 'two', 'three');
$stringToCheck = 'some stringy thing';
// $stringToCheck = 'one stringy thing';
$noBadWordsFound = true;
foreach ($badWords as $badWord) {
if (preg_match("/\b$badWord\b/", $stringToCheck)) {
$noBadWordsFound = false;
break;
}
}
if ($noBadWordsFound) { ... } else { ... }
Problem
Lets say I have an array of bad words: ``` $badwords = array("one", "two", "three"); ``` And random string: ``` $string = "some variable text"; ``` How to create this cycle: ``` if (one or more items from the $badwords array is found in $string) echo "sorry bad word found"; else echo "string contains no bad words"; ``` Example: if `$string = "one fine day" or "one fine day two of us did something"`, user should see sorry bad word found message. If `$string = "fine day"`, user should see string contains no bad words message. As I know, you can't `preg_match` from array. Any advices?