How to Check if a String Contains a Substring in PHP?

Checking whether a string contains a specific substring is a common task in PHP programming.

You might need to perform this operation when searching for keywords in text, validating user input, or manipulating strings.

PHP provides several ways to check if a string contains a substring, and in this article, we'll explore different methods for accomplishing this.

1. Using the strpos() Function

The strpos() function in PHP allows you to find the position of the first occurrence of a substring within a string. If the substring is found, it returns the position as an integer; otherwise, it returns false.

Here's how to use strpos() to check if a string contains a substring:

$string = "This is a sample string.";
$substring = "sample";

if (strpos($string, $substring) !== false) {
    echo "The string contains the substring.";
} else {
    echo "The string does not contain the substring.";
}

In this example, strpos($string, $substring) searches for the $substring within $string. If it returns a value that is not equal to false, it means the substring was found.

2. Using the stripos() Function for Case-Insensitive Search

If you need to perform a case-insensitive search, you can use the stripos() function, which works similarly to strpos() but ignores character case.

$string = "This is a sample string.";
$substring = "SAMPLE";

if (stripos($string, $substring) !== false) {
    echo "The string contains the substring.";
} else {
    echo "The string does not contain the substring.";
}

In this example, the case of the substring doesn't matter, as stripos() performs a case-insensitive search.

3. Using the strstr() Function

The strstr() function can be used to check if a string contains a substring and retrieve the portion of the string that follows the substring.

$string = "This is a sample string.";
$substring = "sample";

if (strstr($string, $substring) !== false) {
    echo "The string contains the substring.";
} else {
    echo "The string does not contain the substring.";
}

In this case, strstr($string, $substring) returns the substring found in the string or false if the substring is not present.

4. Using Regular Expressions with preg_match()

If you need to perform more complex substring searches, regular expressions with the preg_match() function can be a powerful option.

$string = "The quick brown fox jumps over the lazy dog.";
$pattern = "/quick\s+brown/";

if (preg_match($pattern, $string)) {
    echo "The string contains the substring.";
} else {
    echo "The string does not contain the substring.";
}

In this example, the regular expression pattern /quick\s+brown/ searches for the substring "quick brown" with one or more spaces in between. If the pattern is found in the string, preg_match() returns true.

5. Using strpos() for Arrays of Substrings

If you want to check if a string contains any of the substrings from an array, you can loop through the array and use strpos() inside the loop.

$string = "This is a sample string.";
$substrings = ["sample", "test", "example"];

foreach ($substrings as $substring) {
    if (strpos($string, $substring) !== false) {
        echo "The string contains the substring '$substring'.<br>";
    } else {
        echo "The string does not contain the substring '$substring'.<br>";
    }
}

This approach allows you to check multiple substrings against the same string.

Conclusion

Checking if a string contains a substring is a common operation in PHP, and the choice of method depends on your specific requirements. Whether you use strpos() for simple searches, stripos() for case-insensitive searches, strstr() for substring retrieval, preg_match() for complex patterns, or loop through an array of substrings, PHP provides versatile options for handling substring checks in your applications.