Test if a string contains a word in PHP? Test if a string contains a word in PHP? php php

Test if a string contains a word in PHP?


if (strpos($string, $word) === FALSE) {   ... not found ...}

Note that strpos() is case sensitive, if you want a case-insensitive search, use stripos() instead.

Also note the ===, forcing a strict equality test. strpos CAN return a valid 0 if the 'needle' string is at the start of the 'haystack'. By forcing a check for an actual boolean false (aka 0), you eliminate that false positive.


Use strpos. If the string is not found it returns false, otherwise something that is not false. Be sure to use a type-safe comparison (===) as 0 may be returned and it is a falsy value:

if (strpos($string, $substring) === false) {    // substring is not found in string}if (strpos($string, $substring2) !== false) {    // substring2 is found in string}


<?php//  Use this function and Pass Mixed string and what you want to search in mixed string.//  For Example :    $mixedStr = "hello world. This is john duvey";    $searchStr= "john";    if(strpos($mixedStr,$searchStr)) {      echo "Your string here";    }else {      echo "String not here";    }