Test if a string contains a word in PHP?

135,170

Solution 1

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.

Solution 2

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
}

Solution 4

use 

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

Share:
135,170
Lucas Matos
Author by

Lucas Matos

The trouble with the world is that the stupid are cocksure and the intelligent are full of doubt.

Updated on February 11, 2022

Comments

  • Lucas Matos
    Lucas Matos over 2 years

    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 }
    
  • Lucas Matos
    Lucas Matos over 12 years
    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%
  • Admin
    Admin over 12 years
    @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
  • Marc B
    Marc B over 12 years
    @LUcas: should have said so in the question, then. Simple examples get simple answers.
  • Lucas Matos
    Lucas Matos over 12 years
    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
  • Marc B
    Marc B over 12 years
    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.
  • Jonathan Bergeron
    Jonathan Bergeron over 8 years
    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.
  • mickmackusa
    mickmackusa about 3 years
    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.
  • Ajowi
    Ajowi almost 3 years
    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'.