31

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

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

So it echoes true if are is not found in $a.

Community
  • 1
  • 1
Dead Girl
  • 415
  • 1
  • 6
  • 11
  • http://us2.php.net/manual/en/function.strpos.php Documentation works wonders. Self study is brilliance. Trial and Error is a programmers friend. – Rottingham Jan 10 '14 at 17:50

7 Answers7

73

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.

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.

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.

Script47
  • 14,230
  • 4
  • 45
  • 66
Rottingham
  • 2,593
  • 1
  • 12
  • 14
7

Using strstr():

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

Or strpos():

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

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

strpos() is a little faster

Lucas Bustamante
  • 15,821
  • 7
  • 92
  • 86
3

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

if (!strpos($a,'are') !== false) {
    echo 'true';
}
Digital Chris
  • 6,177
  • 1
  • 20
  • 29
  • 3
    why make the double loop on the boolean value?? Why not if (strpos($a, 'are') === false) { // not found } – Rottingham Jan 10 '14 at 17:51
  • @Rottingham I was just following the OP code, but honestly I prefer to avoid testing for the negative, so I can just leave off the equality check and assume use the assumed test for true. For example: `if (!strpos($a,'are') echo 'foo';` and `if (strpos($a,'are') echo 'bar';` }` – Digital Chris Jan 10 '14 at 17:56
  • Not critiquing your code at all by the way! iron sharpens iron, I just like to ask in case I'm missing something better – Rottingham Jan 10 '14 at 17:57
0

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.

0

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.

if (!strstr($mystring, 'Hello')) {
    // $mystring does not contain 'Hello' nowhere, 
    // even not at the beginning of the string
}
else{
    // Your code goes here....
}
H.A.
  • 350
  • 5
  • 14
  • This does not work if the search string is at the end of the string: `if (strstr("43210", "0")) { echo "found 0"; }` – Austin Burk Jan 31 '21 at 23:39
  • 1
    Well that's not what I wrote. I wrote with a !strstr() and not strstr(). So if you want to have the found case, you need to write it with ! and then write your code in the "else" closure. – H.A. Feb 06 '21 at 15:01
0
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'
}
Froslass
  • 48
  • 4
0

I was looking for a solution for my database object and checking to see if the filename in the database object contained a "." If it did it meant all the image upload checks were successful, and then not display the div error.

<?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
    }
?>
Spinstaz
  • 287
  • 6
  • 12