Share via:

Check if string contains a substring with PHP

Check if a string contains a substring using strpos and regular expressions.

1936 views

Edited: 2018-02-06 12:13

Performing a if string contains check in PHP can be done in several ways, one of the best ways is by using strpos(), but it is also possible to check for more complex patterns with regular expressions.

In programming, this problem is usually described as the needle in haystack problem. You may know this figure of speech from everyday life. The source string is the haystack, and the substring is the needle we are searching for.

A common situation where you need to find a string in another string, is when validating e-mail. Either you can use strpos to check for the existence of a "@" character, or you can create a regular expression to perform a more complex validation. PHP has a build-in way for e-mail validation, so creating your own is likely a bad idea.

The below is a simple check using strpos:

if (strpos('string contains PHP', 'PHP') !== false) {
  echo 'PHP was found in string';
} else {
  echo "Not found!";
}

Strpos

The strpos function will either return false if substring is not found, or a numeric value representing it's position if it is found. This is why we use the "!==" operator, which loosely means "not equal to". A value of "0" would also be understood as false, so doing a "!=" comparison will not work. The "!==" operator will however, as it makes PHP do an an exact comparison.

$source_str = 'beamer@beamtic.com';
$substring = '@';

if (strpos($source_str, $substring) !== false) {
  echo 'Found it';
} else {
  echo "Not found!";
}

Note. Again. If $substring is found in $source_str, and it is located in the beginning (the 0'th position), it will return this position. Hence we use "!==" or "<>" as the comparison operator:

$source_str = 'beamer@beamtic.com';
$substring = '@';

if (strpos($source_str, $substring) !== false) {
  echo 'Found it';
} else {
  echo "Not found!";
}

Note. Use stripos for a case-insensitive version of strpos.

Strstr

While Strstr can be used for the same purpose, it is discouraged by the PHP manual, stating that strpos is both faster and uses less memory:

If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.

Using strstr is similar to using strpos:

if (strstr($source_str, $substring) !== false) {
  echo 'Found!';
} else {
  echo 'Not found!';
}

Note. strchr is an alias for strstr

Regular expressions

Regular expressions can be used to match more than just a string, and is generally used when one wants to match a pattern contained within a source string, and not just for simple parts. To use a regex we can use the preg_match function.

Below is a very simple match, which can also be performed using stripos:

if (preg_match("/neeDle/i", "There's a small needle in a haystack")) {
    echo "Found!";
} else {
    echo "Not found!";
}

The i at the end of the pattern /neeDle/i makes the regex perform a case-insensitive match.

Comments