30

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 }
Giacomo1968
  • 24,837
  • 11
  • 67
  • 96
Lucas Matos
  • 1,060
  • 4
  • 23
  • 40
  • 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
  • possible duplicate of [Check if string contains specific words?](http://stackoverflow.com/questions/4366730/check-if-string-contains-specific-words) – Jeroen Jul 06 '15 at 23:13

4 Answers4

82
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
  • 3,826
  • 2
  • 43
  • 64
Marc B
  • 348,685
  • 41
  • 398
  • 480
  • 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
21

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
  • 30,523
  • 8
  • 64
  • 89
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,299
  • 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
0

use 

if(stripos($str,'job')){
   // do your work
}
Ganesh
  • 152
  • 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