Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查字符串是否包含一个单词,是否不包含另一个单词?_Php_Regex_String - Fatal编程技术网

Php 如何检查字符串是否包含一个单词,是否不包含另一个单词?

Php 如何检查字符串是否包含一个单词,是否不包含另一个单词?,php,regex,string,Php,Regex,String,我有这样一个字符串: $str = "this is a test"; ^ # anchor it to the beginning of the line (?:(?!\btest\b).)* # makes sure no test can be matched \bis\b # match is as a word (?:(?!\btest\b).)* # same construct as

我有这样一个字符串:

$str = "this is a test";
^                       # anchor it to the beginning of the line
    (?:(?!\btest\b).)*  # makes sure no test can be matched
    \bis\b              # match is as a word
    (?:(?!\btest\b).)*  # same construct as above
$                       # anchor it to the end of the line
我想验证$str,如果字符串包含is而不包含test,则返回true。我该怎么做

示例:

这是我的模式\bis\b?!测验但它似乎只是检查现有的,我的意思是,当测试存在时,它也会返回true。我的意思是,下面代码的结果是真的,这不应该是真的,因为测试存在

注意:我真的坚持用正则表达式来实现这一点。

使用strpos

$str = "this is a test";
if (strpos($str, 'is') !== false && strpos($str, 'test') === false ) {
   return true;
} else {
  return false;
}
使用strpos

$str = "this is a test";
if (strpos($str, 'is') !== false && strpos($str, 'test') === false ) {
   return true;
} else {
  return false;
}

尝试使用lookahed,无论是正面还是负面:

^(?=.*\bis\b)(?!.*\btest\b).*
解释:

^              # stands for start of the string, both lookahed below will use it as anchor

(?=            # positive lookahed
    .*         # can have any amount of characters till
    \bis\b     # literal text "is" with boundaries
)              # if not succeed will fail the regex

(?!            # negative lookahead
    .*         # can have any amount of characters till
    \btest\b   # literal text "test" with boundaries
)              # if succeed will fail the regex

.*             # if the regex didn't fail till here, match all characters in this line

尝试使用lookahed,无论是正面还是负面:

^(?=.*\bis\b)(?!.*\btest\b).*
解释:

^              # stands for start of the string, both lookahed below will use it as anchor

(?=            # positive lookahed
    .*         # can have any amount of characters till
    \bis\b     # literal text "is" with boundaries
)              # if not succeed will fail the regex

(?!            # negative lookahead
    .*         # can have any amount of characters till
    \btest\b   # literal text "test" with boundaries
)              # if succeed will fail the regex

.*             # if the regex didn't fail till here, match all characters in this line
像^*\b测试\b.*\b\b.*$因此:

if (preg_match ("(^(?!.*\btest\b).*\bis\b.*$)","this is a test")) {
    return true;
} else {
    return false;
}
好的,那么解释吧,虽然很明显,但它首先检查“test”不存在,前面有任何数量的字符,然后确保“is”确实存在

类似于^*\b测试\b.*\b\b.*$因此:

if (preg_match ("(^(?!.*\btest\b).*\bis\b.*$)","this is a test")) {
    return true;
} else {
    return false;
}
好的,那么解释吧,虽然很明显,但它首先检查“test”不存在,前面有任何数量的字符,然后确保“is”确实存在

请试试这个 ^.?\bis\b:\b测试\b.$

请尝试此选项
^.?\bis\b:\b测试\b.$

您可以这样做:

$str = "this is a test";
^                       # anchor it to the beginning of the line
    (?:(?!\btest\b).)*  # makes sure no test can be matched
    \bis\b              # match is as a word
    (?:(?!\btest\b).)*  # same construct as above
$                       # anchor it to the end of the line

有关PHP代码,请参见以下代码段:


提示:请注意,我已在接受答案后更改了答案,以更正原始答案中的缺陷。

您可以这样做:

$str = "this is a test";
^                       # anchor it to the beginning of the line
    (?:(?!\btest\b).)*  # makes sure no test can be matched
    \bis\b              # match is as a word
    (?:(?!\btest\b).)*  # same construct as above
$                       # anchor it to the end of the line

有关PHP代码,请参见以下代码段:


提示:请注意,我已在接受答案后更改了答案,以更正原始答案中的缺陷。

尝试此操作,它可以通过正则表达式正常工作

 $str = "this is a test";

     if (preg_match ("/is/",$str) && !preg_match ("/test/",$str)) {
      return false;
    } else {
        return true;
    }

试试这个,它可以通过正则表达式正常工作

 $str = "this is a test";

     if (preg_match ("/is/",$str) && !preg_match ("/test/",$str)) {
      return false;
    } else {
        return true;
    }

另外,您的解决方案现在不支持word边界。。!另外,您的解决方案现在不支持word边界。。!Downvoter请留言并解释原因?Downvoter请留言并解释原因?OP为什么要尝试这个?一个好的答案总是会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了未来的SO访客。@Shafizadeh。我的错,我输入了=两次,我已经更新了为什么OP要尝试这个?一个好的答案总是会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了未来的SO访客。@Shafizadeh。我的错,我输入了=两次,我已经更新了为什么OP要尝试这样的东西?一个好的答案总是会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了未来的访客。这个答案与我想要的非常接近。。thx+1。。请在你的模式上应用\b。啊,是的,很抱歉我错过了@Shafizadeh。现在加上:为什么OP要尝试这样的事情?一个好的答案总是会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了未来的访客。这个答案与我想要的非常接近。。thx+1。。请在你的模式上应用\b。啊,是的,很抱歉我错过了@Shafizadeh。现在加上:实际上,我正试图通过一个集成模式来实现这一点。。也请添加!在第二个条件的开头:&&!preg_match“/test/”,$a您可以使用反勾号包装文本,将其格式化为代码。实际上,我正试图通过一种集成模式来实现这一点。。也请添加!在第二个条件的开头:&&!preg_match“/test/”,$a您可以使用反勾号包装文本,将其格式化为代码。