Lewis Smith
Lewis Smith

Reputation: 1345

PHP strpos not returning true when the items match

This is going to sound really simple but I can't understand why its not working.

I am checking that a string matches something to enable some texts to be sent.

The string that is returned is: Delivered to UPS Access Point™ location and awaiting customer pickup.

And this is the statement I am using to check for it

if(strpos(strtolower($step->status), "delivered to ups access point")) {

}

Currently that if statement is returning false when I would expect it to be true

Any ideas

Upvotes: 0

Views: 139

Answers (3)

Vamsi
Vamsi

Reputation: 421

This should work

$step->status = "Delivered to UPS Access Point™ location and awaiting customer pickup.";
if(strpos(strtolower($step->status), "delivered to ups access point") !== false) {
    echo "String matches";
}
else {
    echo "String not matches";
}

Upvotes: 1

DevionNL
DevionNL

Reputation: 153

You are treating the strpos as if it's a boolean, which is it not. It returns the position of the string which would be 0, and translates to false.

So compare against not false (not found) or >= 0 (found), type is important.

Example:

    $data = "Delivered to UPS Access Point™ location and awaiting customer pickup.";
    if(strpos(strtolower($data), "delivered to ups access point") !== false) {
     echo 'Found!';
    }

Upvotes: 5

KP Pankhaniya
KP Pankhaniya

Reputation: 40

strpos is return index of your second argument. In your case "delivered to ups access point" is starting point mean 0th position. 0 mean false.

Upvotes: 1

Related Questions