Can anyone suggest how I would do this: Say if I had the string $text
which holds text entered by a user. I want to use an 'if statement' to find if the string contains one of the words $word1
$word2
or $word3
. Then if it doesn't, allow me to run some code.
if ( strpos($string, '@word1' OR '@word2' OR '@word3') == false ) {
// Do things here.
}
I need something like that.
More flexibile way is to use array of words:
$text = "Some text that containts word1";
$words = array("word1", "word2", "word3");
$exists = false;
foreach($words as $word) {
if(strpos($text, $word) !== false) {
$exists = true;
break;
}
}
if($exists) {
echo $word ." exists in text";
} else {
echo $word ." not exists in text";
}
Result is: word1 exists in text