如何使用 php 检查字符串中是否存在单词

How to check if word exist in a string with php

如何检查字符串中是否存在单词。

我尝试了 strpos 但效果不佳。

我的例子 if(strpos($string, 'As ') !== false)

字符串是:

"Asthmatic is ruff"

然后它给了我真实的,因为包含了 As。

但我需要找到单词

"As" or "as"

示例:

"As i walked" or "If it was as"

if (preg_match('/\b(as)\b/i', $string, $matches)) {
    //$string had the word 'as' in it
}

使用preg_match匹配正则表达式。 \b 表示单词边界。 i 表示匹配不区分大小写

试试 stripos(不区分大小写的 strpos 版本)