php 如何检查字符串是否不包含特定短语?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21051004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:46:21  来源:igfitidea点击:

How to check if a string does not contain a specific phrase?

php

提问by Dead Girl

How to invert the function of How do I check if a string contains a specific word in PHP?

如何反转PHP如何检查字符串是否包含特定单词的功能?

if (strpos($a,'are') !== false) {
    echo 'true';
}

So it echoes trueif areis notfound in $a.

因此,它呼应true,如果are中找到$a

回答by Rottingham

The code here:

这里的代码:

if (strpos($a, 'are') !== false) {
    // The word WAS found
}

Means that the word WAS found in the string. If you remove the NOT (!) operator, you have reversed the condition.

表示在字符串中找到单词 WAS。如果删除 NOT (!) 运算符,则条件已颠倒。

if (strpos($a, 'are') === false) {
    // The word was NOT found
}

the === is very important, because strpos will return 0 if the word 'are' is at the very beginning of the string, and since 0 loosely equals FALSE, you would be frustrated trying to find out what was wrong. The === operator makes it check very literally if the result was a boolean false and not a 0.

=== 非常重要,因为如果单词 'are' 位于字符串的最开头,strpos 将返回 0,并且由于 0 大致等于 FALSE,您会因试图找出错误而感到沮丧。=== 运算符使它非常严格地检查结果是否是布尔值 false 而不是 0。

As an example,

举个例子,

if (!strpos($a, 'are')) {
    // String Not Found
}

This code will say the string 'are' is not found, if $a = "are you coming over tonight?", because the position of 'are' is 0, the beginning of the string. This is why using the === false check is so important.

这段代码会说没有找到字符串'are',如果$a = "你今晚要过来吗?",因为'are'的位置是0,字符串的开头。这就是为什么使用 === false 检查如此重要的原因。

回答by Lucas Bustamante

Using strstr():

使用 strstr():

if (!strstr($var, 'something')) {
    // $var does not contain 'something'
}

Or strpos():

或 strpos():

if (strpos($var, 'something') === false) {
    // $var does not contain 'something'
} 

Or stripos() if you want case-insensitive search.

或者 str ipos() 如果你想要不区分大小写的搜索。

strpos() is a little faster

strpos()一点

回答by Digital Chris

You'll probably kick yourself when you see it...

当你看到它时,你可能会踢自己......

if (!strpos($a,'are') !== false) {
    echo 'true';
}

回答by Pratik Maniar

Try this

尝试这个

$string = "This is beautiful world.";
$$string = "Beautiful";
    preg_match('/\b(express\w+)\b/', $string, $x); // matches expression

    \b is a word boundary
    \w+ is one or more "word" character
    \w* is zero or more "word" characters
    enter code here

See the manual on escape sequencesfor PCRE.

请参阅有关PCRE转义序列的手册。

回答by H.A.

strpos() !== false gives you a wrong return value, if the search-string is at the beginning of the string. So you better use strstr(), which gives you an accurate result.

strpos() !== false 给你一个错误的返回值,如果搜索字符串在字符串的开头。所以你最好使用 strstr(),它会给你一个准确的结果。

if (!strstr($mystring, 'Hello')) {
    // $mystring does not contain 'Hello' nowhere, 
    // even not at the beginning of the string
}