dp7311
2016-05-31 13:00如何检查字符串是否包含一个单词并且不包含另一个单词?
I have a string like this:
$str = "this is a test";
I want to validate $str
and return true if string is containing is
and it isn't containing test
. How can I do that?
Examples:
"this is a test" // false
"this is a tes" // true "is" exists and "test" doesn't exist
"this iss a tes" // false
"this iss a test" // false
Here is my pattern \bis\b(?!test)
. But it seems to just checks existing, I mean it also returns true when test
exists. I mean the result of following code us true which shouldn't be (because test
exists).
if (preg_match ("/\bis\b(?!test)/","this is a test")) {
return true;
} else {
return false;
}
Note: I'm really insist on doing that by regex.
- 点赞
- 回答
- 收藏
- 复制链接分享
6条回答
为你推荐
- 包含在不使用array-PHP的情况下连接的单词的回显字符串
- loops
- string
- echo
- php
- 2个回答
- 正则表达式分裂字符串,如果它包含wordS或单词[关闭]
- regex
- php
- 3个回答
- 将有多个字符串和空格组成的字符串分离成单独的字符串
- c++
- 1个回答
- PHP,我想计算一个字符串中包含六个以上字母的单词数
- php
- 2个回答
- 从仅包含URL字符串中的特定单词的文本中获取图像URL
- url
- parsing
- match
- php
- 1个回答
换一换