【问题标题】:How to check if string contains two or more specific words如何检查字符串是否包含两个或多个特定单词
【发布时间】:2019-03-11 18:40:18
【问题描述】:

我需要检查字符串是否包含两个或多个特定的单词,如果找到回显单词..我尝试使用这个..但是如果找到一个单词,那么它回显找到并且它现在可以工作:

试用代码:

if(preg_match("/(s|g898|w63)/i", $string)){
    echo 'found';
}

所以我尝试使用这个例子:

$string = 'Today is wonderfull day w63';

上面的函数什么都不用做。

$string = 'Above the sky limit s g898 w63';

这个字符串需要从上面的函数echo 'found'回显。

那么如何在 PHP 中检查字符串是否包含所有三个特定单词并且是否包含所有三个找到的回声?

【问题讨论】:

  • 您是否尝试将您要查找的单词放入一个数组并循环遍历该数组?
  • 同一个词出现两次怎么办?这算两个字吗? php.net/manual/en/function.preg-replace.php 和第五个参数浮现在脑海中
  • strpos 第一个词,strpos 第二个词。

标签: php string


【解决方案1】:

这是一种可能的解决方案

if(preg_match("/(?=.* s )(?=.*g898)(?=.*w63)/i", $string)){
    echo 'found';
}

【讨论】:

  • 单词边界会改善这一点。
猜你喜欢
  • 1970-01-01
  • 2011-05-20
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多