Does String Contain String

strpos()

strpos ($haystack , $needle , $offset = 0 )

Returns False on no match or first occurrence index (from 0) on match

  if (strpos($my_string, 'hello') !== False)
  {
    echo 'true';
  }
  $char_index = strpos( strtolower($StringToCheck),  strtolower($LookingForString));		//Using strtolower() removes case sensitivity
  if($char_index === false)
  {
    //Not found
  }
  else
  {
    //Found
  }

Does string start with

  if (strpos( strtolower($StringToCheck), strtolower($LookingForString)) === 0)     //False on no match, first occurance index on match
Alternative substring compare method
  if (substr($StringToCheck, 0, 2) === $LookingForString)

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *