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 }
1
  • 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. Commented Feb 2, 2012 at 20:05

5 Answers 5

89
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.

Sign up to request clarification or add additional context in comments.

6 Comments

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%
@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
@LUcas: should have said so in the question, then. Simple examples get simple answers.
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
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.
|
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
}

Comments

1

use 

if(stripos($str,'job')){
   // do your work
}

1 Comment

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.
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";
    }

1 Comment

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.
-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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.