33

In SQL we have NOT LIKE %string%

I need to do this in PHP.

if ($string NOT LIKE %word%) { do something }

I think that can be done with strpos()

But can’t figure out how…

I need exactly that comparission sentence in valid PHP.

if ($string NOT LIKE %word%) { do something }
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Lucas Matos
  • 1,112
  • 5
  • 25
  • 42
  • I updated the title to better reflect the question. The exact semantics of word matching should also be laid out. It may be better to use `\bword\b` or `\bword|word\b` in certain situations. –  Feb 02 '12 at 20:05

5 Answers5

88
if (strpos($string, $word) === FALSE) {
   ... not found ...
}

Note that strpos() is case sensitive, if you want a case-insensitive search, use stripos() instead.

Also note the ===, forcing a strict equality test. strpos CAN return a valid 0 if the 'needle' string is at the start of the 'haystack'. By forcing a check for an actual boolean false (aka 0), you eliminate that false positive.

Kariem
  • 4,398
  • 3
  • 44
  • 73
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • note that %word% is a wildcard, not a variable... the $string contains an IP address in char form, like $string = 123.456.789.100, and i want to exclude (NOT LIKE) those that start with 123.456% – Lucas Matos Feb 02 '12 at 20:06
  • @LucasMatos Then `$word = "word"` then ... while it is a wildcard, it's a very trivial usage ("floating ends") that `strpos` covers (and in fact it is *because* of this that `strpos` works here!). `$word = "a?b"` would not work with this approach, if `?` was meant to "match any character", for instance –  Feb 02 '12 at 20:10
  • 2
    @LUcas: should have said so in the question, then. Simple examples get simple answers. – Marc B Feb 02 '12 at 20:14
  • thats how my sentence looks like `if ($viewer_ip AND $viewer_ip != $last_viewer_ip AND strpos($viewer_ip, 66.249) === false) {` i want to exclude google´s bot ips from my counter. just have to wait one of them reach my site to check if its working porperly. thanks u all – Lucas Matos Feb 02 '12 at 20:25
  • be very careful with a 'bare' floating point number like that. PHP may stringify it with different trailing decimals than you'd expect. Force it to be a string with `66.249` instead, and not that it WILL find things like `166.249`. If that's the at the START of the string, you can change the `===` to a simple `>` instead, to exclude any matches at the start. – Marc B Feb 02 '12 at 21:46
  • and just a note, the substring being looked for in these __strpos and stripos__ should be an actual __word__ like 'this __name__ is reachable and not the _Name_ in __thisName__'. – Ajowi Jul 17 '21 at 17:09
22

Use strpos. If the string is not found it returns false, otherwise something that is not false. Be sure to use a type-safe comparison (===) as 0 may be returned and it is a falsy value:

if (strpos($string, $substring) === false) {
    // substring is not found in string
}

if (strpos($string, $substring2) !== false) {
    // substring2 is found in string
}
TimWolla
  • 31,849
  • 8
  • 63
  • 96
1

use 

if(stripos($str,'job')){
   // do your work
}
Ganesh
  • 162
  • 1
  • 9
  • 1
    This will fail when `job` is the leading three characters of `$str`. Correcting this unexplained snippet will make it a duplicate answer on this page. This post is safe to remove. – mickmackusa Apr 22 '21 at 02:42
0
<?php
//  Use this function and Pass Mixed string and what you want to search in mixed string.
//  For Example :
    $mixedStr = "hello world. This is john duvey";
    $searchStr= "john";

    if(strpos($mixedStr,$searchStr)) {
      echo "Your string here";
    }else {
      echo "String not here";
    }
Vishnu Sharma
  • 1,357
  • 12
  • 11
  • 7
    This will fail if $searchStr is at the beggining of $mixedStr. In that example, if you search for "hello", you will echo "String not here", cause strpos will return 0, which will branch into the else condition. Always use === when checking the return value of strpos. – Jonathan Bergeron Jan 28 '16 at 16:36
-1

Kind of depends on your data, doesn't it? strpos('a foolish idea','fool') will show a match, but may not be what you want. If dealing with words, perhaps

preg_match("!\b$word\b!i",$sentence)

is wiser. Just a thought.

Cuse70
  • 271
  • 3
  • 5
  • What if `$word` contains special characters? This answer shows how to match a whole word. The question asked how to determine if a substring is not present in an input string. – mickmackusa Jan 31 '23 at 04:13