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

8

29
$filename = 'my_upgrade(1).zip';
$match = 'my_upgrade';

if(!strpos($filename, $match))
    {
    die();
    }
else 
   {
   //proceed
   }

In the code above, I'm trying to die out of the script when the filename does not contain the text string "my_upgrade". However, in the example given, it should not die since "my_upgrade(1).zip" contains the string "my_upgrade".

What am I missing?

Occult answered 8/8, 2011 at 19:28 Comment(1)
strpos returns the numeric position of the match. Not sure if that helps.Marengo
N
64

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.

Narbada answered 8/8, 2011 at 19:30 Comment(0)
L
11

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)

Leafstalk answered 24/6, 2015 at 7:34 Comment(0)
G
5
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)

Geostatics answered 8/8, 2011 at 19:30 Comment(0)
C
2
false === strpos($filename, $match)

The strpos functionDocs returns false if not found or 0 if found on position 0 (programmers like to start counting at 0 often):

Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Civilly answered 8/8, 2011 at 19:30 Comment(0)
D
1

strpos returns false if the string is not found, and 0 if it is found at the beginning.

so try this:

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

Note we use a strict comparison (===) If we don't, both types (false and false or an integer) will be coerced to the same type, and 0 when coerced to a bool is false.

more details: https://zdevtips.com/guid/strpos-in-php-example4-example-codes/

Dogs answered 4/3, 2021 at 11:57 Comment(1)
Why are you answering a question with the same answer as others already provided like 9 years ago?Thebault
A
0

This working for me when everything other fail in some situations:

$filename = 'my_upgrade(1).zip';
$match = 'my_upgrade';
$checker == false;
if(strpos($filename, $match))
    {
    $checker == true;
    }
if ($checker === false)
{ 
die();
}
else 
{
//proceed
}

Or in short:

$filename = 'my_upgrade(1).zip';
$match = 'my_upgrade';
$checker == false;
if(strpos($filename, $match))
    {
    $checker == true;
//proceed
 }
if ($checker === false)
{ 
die();
}
Assembled answered 22/1, 2019 at 9:27 Comment(0)
C
0

$data = 'match';
$line = 'match word in this line';
if(strpos($line,$data) !== false){
    echo "data found";
}else{
    echo "no data found";
}
Carriecarrier answered 6/11, 2019 at 10:29 Comment(0)
M
-1

strpos in this case will return a zero, which is then interpretted as false when you do the logical negation. You should check explicitly for the boolean false.

Manganese answered 8/8, 2011 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.