How to use strpos to determine if a string exists in input string? How to use strpos to determine if a string exists in input string? php php

How to use strpos to determine if a string exists in input string?


strpos returns false if the string is not found, and 0 if it is found at the beginning. Use the identity operator to distinguish the two:

if (strpos($filename, $match) === false) {

By the way, this fact is documented with a red background and an exclamation mark in the official documentation.


The strpos() function is case-sensitive.

if(strpos($filename, $match) !== false)        {        // $match is present in $filename        }    else        {       // $match is not present in $filename        }

For using case-insensitive. use stripos() that is it finds the position of the first occurrence of a string inside another string (case-insensitive)


if (strpos($filename, $match) === false)

Otherwise, strpos will return 0 (the index of the match), which is false.

The === operator will also compare type of the variables (boolean != integer)