PHP - How to check if string contains substring PHP

/ Published in: PHP
Save to your folder(s)

Once in a while we need to check whether a string contains substring, some other string or characters or a value. Checking for existence of a string (or substring) inside another string is easier than it might seem. The following article describes how to check for whether a string contains string in PHP, or simply said how to find string within string in PHP.

Copy this code and paste it in your HTML
  1. $intPos = strpos($strHaystack,$strNeedle);
  3. if( $intPos === false ) {
  4. // string strNeedle NOT found in strHaystack
  5. } else {
  6. // string strNeedle found in strHaystack
  7. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.