简体   繁体   English

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

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

How to invert the function of How do I check if a string contains a specific word in PHP?如何反转How do I check if a string contains a specific word in PHP的function?

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

So it echoes true if are is not found in $a .因此,如果在$a找不到are ,它会返回true

The code here:这里的代码:

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

Means that the word WAS found in the string.表示在字符串中找到单词 WAS。 If you remove the NOT (!) operator, you have reversed the condition.如果删除 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. === 非常重要,因为如果单词 'are' 位于字符串的最开头,strpos 将返回 0,并且由于 0 大致等于 FALSE,您会因试图找出错误而感到沮丧。 The === operator makes it check very literally if the result was a boolean false and not a 0. === 运算符使它非常严格地检查结果是否是布尔值 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.这段代码会说没有找到字符串'are',如果$a = "你今晚要过来吗?",因为'are'的位置是0,字符串的开头。 This is why using the === false check is so important.这就是为什么使用 === false 检查如此重要的原因。

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 str i pos() if you want case-insensitive search.或者 str i pos() 如果你想要不区分大小写的搜索。

strpos() is a little faster strpos() 一点

You'll probably kick yourself when you see it...当你看到它时,你可能会踢自己......

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

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 sequences for PCRE.请参阅有关 PCRE转义序列的手册。

strpos() !== false gives you a wrong return value, if the search-string is at the beginning of the string. strpos() !== false 给你一个错误的返回值,如果搜索字符串在字符串的开头。 So you better use strstr(), which gives you an accurate result.所以你最好使用 strstr(),它会给你一个准确的结果。

if (!strstr($mystring, 'Hello')) {
    // $mystring does not contain 'Hello' nowhere, 
    // even not at the beginning of the string
}
function find_word($text, $word) {
/*yuHp*/
$sizeText = strlen($text);
$sizeWord = strlen($word);
$text = strtolower($text);
$word = strtolower($word);
$contadorText = 0;
$pontuacao = 0;
while ($contadorText < $sizeText) {
    if ($text[$contadorText] == $word[$pontuacao]) {
        $pontuacao++;
        if ($pontuacao == $sizeWord) {
            return true;
        }
    } else {
        $pontuacao = 0;
    }
    $contadorText++;
}

return false;
}

if (!find_word('today', 'odf')){
   //did not find 'odf' inside 'today'
}

I was looking for a solution for my database object and checking to see if the filename in the database object contained a "."我正在为我的数据库 object 寻找解决方案,并检查数据库 object 中的文件名是否包含“.”。 If it did it meant all the image upload checks were successful, and then not display the div error.如果是,则表示所有图片上传检查都成功,然后不会显示 div 错误。

<?PHP
 } else if ( str_contains('$gigObject->getPhoto()','.' ) !== false) {
   ?>
  <div class="bottom" id="photoHeaderError" style="display:block">
    <div class="bottom_error_alert">
     Image is missing. Please upload one for your gig.
    </div>

  </div>

  <?php
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM