Search code examples
phpstrposstripos

strpos/stripos (with strict comparison) failing


I currently have the following strpos/stripos (tried both, doesn't change outcome), used to flag certain needles from forum posts.

if (stripos($row->message, (string)$commit) !== false) {
  // Do stuff
}

Stuff mostly works fine, however, in a few cases when haystack ($row->message) contains needle ($commit) (using cast because $commit can be an int) strpos/stripos is returning false.

For example:

$test = stripos($row->message, (string)$commit);

An example of a non-working scenario is:

// (string)$commit 
string(7) "818df50"
// $row->message
string(321) "Intro. Crashes as soon as the main menu is shown. Graphics are horribly broken, no fixes have been found as of yet. [attachment=194] Build Used: Pull Request #3333 (0.0.3-5929) -> RPCS3-v0.0.3-2017-08-27-818f50b Side note: States it can display at 1080p, yet it is boxed in 1080p. However, it is fine in 720p."
// $test (strpos/stripos) 
bool(false)

An example of a working scenario is:

// (string)$commit
string(7) "2570911" 
// $row->message
string(219) "RPCS3 v0.0.3-3-2570911 Alpha The game doesn't even show its graphics window. [quote] ·F 0:00:34.138812 {PPU[0x1000000] Thread (main_thread) [0x0087c094]} MEM: Access violation writing location 0xcffff2d0 [/quote]" int(15) [2570911]
// $test (strpos/stripos)
int(15) [2570911]

I don't know if I'm missing something really obvious, I checked a few times and approach seems correct.

Note: I know there are dozens of ways this can be done, but in this specific scenario I need these string comparisons.

Thanks in advance.


Solution

  • In your example...

    // (string)$commit 
    string(7) "818df50"
    // $row->message
    string(321) "Intro. Crashes as soon as the main menu is shown. Graphics are horribly broken, no fixes have been found as of yet. [attachment=194] Build Used: Pull Request #3333 (0.0.3-5929) -> RPCS3-v0.0.3-2017-08-27-818f50b Side note: States it can display at 1080p, yet it is boxed in 1080p. However, it is fine in 720p."
    // $test (strpos/stripos) 
    bool(false)
    

    The needle 818df50 is not contained in the haystack. The haystack contains a very close string (818f50) but that is not an exact match.

    So... it's doing the correct operation.