测试一个字符串是否包含PHP中的单词?
In SQL we have NOT LIKE %string%
在SQL中我们没有像%string%
i need to do this in PHP.
我需要在PHP中执行此操作。
if ($string NOT LIKE %word%) { do something }
if($ string NOT LIKE%word%){做某事}
i think that can be done with strpos()
我认为可以用strpos()完成
but couldnt figure out how..
但无法弄清楚如何......
i need exactly that comparission sentence in valid PHP.
我需要在有效的PHP中完全相同的comparission句子。
if ($string NOT LIKE %word%) { do something }
if($ string NOT LIKE%word%){做某事}
Thanks
谢谢
4 个解决方案
#1
54
if (strpos($string, $word) === FALSE) {
... not found ...
}
Note that strpos()
is case sensitive, if you want a case-insensitive search, use stripos()
instead.
请注意,strpos()区分大小写,如果您想要不区分大小写的搜索,请使用stripos()。
Also note the ===
, forcing a strict equality test. strpos CAN return a valid 0
if the 'needle' string is at the start of the 'haystack'. By forcing a check for an actual boolean false (aka 0), you eliminate that false positive.
还要注意===,强制进行严格的相等测试。 strpos如果'needle'字符串位于'haystack'的开头,则返回有效0。通过强制检查实际的布尔值false(也就是0),可以消除误报。
更多相关文章
- 几个有用的php字符串过滤,转换函数代码
- PHP - 将字符串转换为键值数组
- 使用mod_rewrite将文件夹转换为查询字符串
- 计算字符串的MD5哈希值
- 有一种防弹的方法可以检测php字符串中的base64编码吗?
- 如何在mysql中配置区分大小写的数据库标识符
- 在两个不同的地方分割/爆炸一个PHP字符串
- PHP中的嵌套(多个)字符串插值
- 查找具有特定数据字符串的数组并返回其具有的另一个数据字符串