如何在PHP中检查字符串是否包含特定的单词?

我有一个字符串,想检查字符串是否提供了一些我想看的单词。例如,字符串如下所示

null
$mypoem="This is a poem from poftut.com to its readers";

有不同的方式来存档这个我们将看看下面。

通过使用strpos()函数

strpos是一个Php 用于在字符串中查找给定单词位置的函数。如果单词exist,strpos将返回单词在字符串中的起始位置;如果不存在,则返回-1。在这个例子中,我们将看到字符串 poem 在字符串变量中 $mypoem .

$mypoem="This is a poem from poftut.com to its readers";if (strpos($mypoem, 'poem') !== false) { echo 'true';}

通过使用pregu match()函数

pregu match是一个正则表达式匹配,它提供了更多的细节来检查。我们可以看复杂的单词,但现在我们只看简单的单词匹配。 在这个例子中,我们将看到字符串 poem 在字符串变量中 $mypoem .

$mypoem="This is a poem from poftut.com to its readers";if (preg_match('/poem/',$mypoem)) echo 'true';

如何在PHP中检查字符串是否包含特定的单词?信息图

How To Check If String Contains A Specific Word in PHP? Infografic
如何在PHP中检查字符串是否包含特定的单词?信息图

相关文章: C Printf()函数教程及示例

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享