Tuesday 7 July 2015

Check if string contains specific words - PHP

Check if string contains specific words - PHP


strpos: Find the position of the first occurrence of a substring in a string.

strpos have 3 parameter and are following:
  1. haystack:The string to search in.
  2. needle:If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
  3. offset: It is optional, If provided then search will start this number of characters counted from the beginning of the string.
  4. The offset cannot be negative.

Note: It is case sensitive.


Different Examples of strpos
var_dump(strpos('web technology experts','web'));//int(0)
var_dump(strpos('web technology experts','technology'));//int(4)
var_dump(strpos('web technology experts','Technology'));//int(4)
var_dump(strpos('web technology experts','technology',4));//int(4)
var_dump(strpos('web technology experts','technology',5));//bool(false)
var_dump(strpos('web technology experts','expertss',6));//bool(false)