1$myString = 'Hello Bob how are you?';
2if (strpos($myString, 'Bob') !== false) {
3 echo "My string contains Bob";
4}
1$a = 'How are you?';
2
3if (strpos($a, 'are') !== false) {
4 echo 'true';
5}
1$result = strpos("haystack", "needle");
2
3if ($result != false)
4{
5 // text found
6}
1// returns true if $needle is a substring of $haystack
2function contains($haystack, $needle){
3 return strpos($haystack, $needle) !== false;
4}
1// this method is new with PHP 8 and above
2$haystack = "Damn, I wonder if this string contains a comma.";
3if (str_contains($haystack, ",")) {
4 echo "There is a comma!!";
5}