php - strpos/stripos(严格比较)失败

标签 php strpos stripos

我目前有以下 strpos/stripos(都试过了,结果没有改变),用于标记论坛帖子中的某些针头。

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

Stuff 大部分工作正常,但是,在少数情况下,当 haystack ($row->message) 包含 needle ($commit)(使用 cast,因为 $commit 可以是一个 int)strpos/stripos 返回 false。

例如:

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

一个非工作场景的例子是:

// (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)

一个工作场景的例子是:

// (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]

我不知道我是否遗漏了一些非常明显的东西,我检查了几次,方法似乎是正确的。

注意:我知道有很多方法可以做到这一点,但在这个特定场景中我需要这些字符串比较。

提前致谢。

最佳答案

在你的例子中......

// (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)

大海捞针 818df50 不在大海捞针中。 haystack 包含一个非常接近的字符串 (818f50),但这不是完全匹配。

所以...它正在执行正确的操作。

关于php - strpos/stripos(严格比较)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45928378/

相关文章:

PHP PDO mysql 按结果 ID 获取行

php - 在第二次出现一个字符之后和最后一次出现相同字符之前返回一个字符串与php

api - Facebook fql 不区分大小写的 strpos 版本

php - 检查url是否包含相同的字符串

php - 评估 stripos(),!== FALSE 和 === TRUE 之间有什么区别?

php - 如何创建 URL 提取器,如 facebook 共享

javascript - 使用 javascript 和 php 登录帐户时无法重定向

javascript - 从 Javascript 中的 PHP 数组填充 Google GeoChart map 的问题