测试字符串是否包含 PHP 中的单词? [英] Test if a string contains a word in PHP?

查看:29
本文介绍了测试字符串是否包含 PHP 中的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SQL 中,我们有 NOT LIKE %string%

In SQL we have NOT LIKE %string%

我需要在 PHP 中执行此操作.

I need to do this in PHP.

if ($string NOT LIKE %word%) { do something }

我认为可以用 strpos()

但不知道怎么...

我需要有效的 PHP 中的比较语句.

I need exactly that comparission sentence in valid PHP.

if ($string NOT LIKE %word%) { do something }

推荐答案

if (strpos($string, $word) === FALSE) {
   ... not found ...
}

请注意,strpos() 是大小写敏感,如果您想要不区分大小写的搜索,请使用 stripos() 代替.

Note that strpos() is case sensitive, if you want a case-insensitive search, use stripos() instead.

还要注意 ===,强制进行严格的相等测试.如果 'needle' 字符串位于 'haystack' 的开头,strpos 可以返回一个有效的 0.通过强制检查实际布尔值 false(即 0),您可以消除误报.

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.

这篇关于测试字符串是否包含 PHP 中的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆