The PHP language provides several functions to manipulate strings. One of the most frequently used string operations is checking whether a PHP string contains another string.
Table Of Contents
PHP applications commonly use this operation to search for a substring in a larger string or determine if a string contains a specific character or pattern.

In this article, we will explore various techniques and functions to perform string contains operation in PHP.
Using the strpos Function
The strpos function is a built-in PHP function that returns the position of the first occurrence of a substring in a string. It takes two arguments: the string to search in, and the substring to search for. The function returns false if it cannot find the substring or the position of its first occurrence if it is found.
Here is an example of using the strpos function:
<?php
$string = "This is a sample string.";
$substring = "sample";
if (strpos($string, $substring) !== false) {
echo "The substring '$substring' was found in the string.";
} else {
echo "The substring '$substring' was not found in the string.";
}
In this example, we first define a string and a substring to search for. We then use the strpos function to search for the substring in the string. We use the !==
operator to compare the return value of the function to false, because strpos can return 0 if the substring is found at the beginning of the string, which would be considered a false positive if we used the ==
operator.
If the substring is found, we print a message indicating that it was found; otherwise, we print a message indicating that it was not found.
Using the stripos Function
The stripos function is similar to strpos, but it performs a case-insensitive search. This means that it will find the substring regardless of whether the case of the characters in the substring matches the case of the characters in the string.
Here is an example of using the stripos function:
<?php
$string = "This is a sample string.";
$substring = "SaMpLe";
if (stripos($string, $substring) !== false) {
echo "The substring '$substring' was found in the string.";
} else {
echo "The substring '$substring' was not found in the string.";
}
In this example, we use the stripos function to search for the substring “SaMpLe” in the string. Even though the case of the characters in the substring does not match the case of the characters in the string, the function still finds the substring because it performs a case-insensitive search.
Using the strstr Function
The strstr function is another built-in PHP function that searches for a substring in a string. It takes two arguments: the string to search in, and the substring to search for. If the substring is found, the function returns the part of the string that follows the substring. In case the substring is not found, the function will return false.
Here is an example of using the strstr function:
<?php
$string = "This is a sample string.";
$substring = "sample";
if (strstr($string, $substring)) {
echo "The substring '$substring' was found in the string.";
} else {
echo "The substring '$substring' was not found in the string.";
}
In this example, we use the strstr function to search for the substring “sample” in the string. If the substring is found, the function returns the part of the string that follows the substring, which is considered a true value in PHP. When the substring cannot be found, the function will return false, which is considered a false value in PHP.
Using the stristr Function
The stristr function is similar to strstr, but it performs a case-insensitive search like stripos. It takes two arguments: the string to search in, and the substring to search for. If the substring is found, the function returns the part of the string that follows the substring. When the substring cannot be found, the function will return false.
Here is an example of using the stristr function:
<?php
$string = "This is a sample string.";
$substring = "SaMpLe";
if (stristr($string, $substring)) {
echo "The substring '$substring' was found in the string.";
} else {
echo "The substring '$substring' was not found in the string.";
}
In this example, we use the stristr function to search for the substring “SaMpLe” in the string. The function returns the part of the string that follows the substring because it is found. Even though the case of the characters in the substring does not match the case of the characters in the string, the function still finds the substring because it performs a case-insensitive search.
Using Regular Expressions
Regular expressions provide a powerful and flexible way to search for patterns in strings. In PHP, regular expressions can be used with the preg_match function to perform string contains operation.
The preg_match function takes two arguments: the regular expression pattern to search for, and the string to search in. If the pattern is found in the string, the function returns 1. The function returns 0 if the pattern is not found.
Here is an example of using regular expressions with the preg_match function:
<?php
$string = "This is a sample string.";
$pattern = "/sample/i";
if (preg_match($pattern, $string)) {
echo "The pattern '$pattern' was found in the string.";
} else {
echo "The pattern '$pattern' was not found in the string.";
}
Furthermore, In this example we define a regular expression pattern that searches for the substring “sample” in a case-insensitive manner. We use the preg_match function to search for the pattern in the string. If the pattern is found, the function returns 1, which is considered a true value in PHP. If the pattern is not found, the function returns 0, which is considered a false value in PHP.
Using the in_array Function
The in_array function is a PHP function that checks if a value exists in an array. It can also be used to check if a substring exists in a string by converting the string to an array of characters using the str_split function.
Here is an example of using the in_array function to check if a substring exists in a string:
<?php
$string = "This is a sample string.";
$substring = "sample";
if (in_array($substring, str_split($string))) {
echo "The substring '$substring' was found in the string.";
} else {
echo "The substring '$substring' was not found in the string.";
}
In this example, we first use the str_split function to convert the string to an array of characters. We then use the in_array function to check if the substring “sample” exists in the array of characters.
If the substring is found, the function returns true, which is considered a true value in PHP. When the substring cannot be found, the function will return false, which is considered a false value in PHP.
Using the str_contains Function
str_contains
is a newer function in PHP that was introduced in PHP 8.0. It is a simple and efficient way to check if a string contains a specific substring.
Here is an example of using the str_contains
function:
<?php
$string = "The quick brown fox jumps over the lazy dog";
$substring = "fox";
if (str_contains($string, $substring)) {
echo "The substring '$substring' was found in the string.";
} else {
echo "The substring '$substring' was not found in the string.";
}
In this example, we use the str_contains
function to check if the string contains the substring “fox”. If the script finds the substring in the string, it outputs a message saying so. If not, it outputs a message saying the substring was not found.
The str_contains
function is very simple to use and can be a great choice for basic string contains operations. However, it should be noted that this function is case-sensitive. If you need to perform a case-insensitive search, you can use the stripos
function or the stristr
function, as discussed in the previous examples.
Using the strpos and substr Functions Together
In another hand, we can use the strpos and substr functions together to extract it from the string. This technique can be useful in situations where you need to extract a specific piece of information from a larger string.
Here is an example of using the strpos and substr functions together:
<?php
$string = "The date is 09/02/2023";
$substring = "/";
if (strpos($string, $substring)) {
$date = substr($string, strpos($string, $substring) + 1);
echo "The date is $date.";
} else {
echo "The substring '$substring' was not found in the string.";
}
In this example, the strpos function is used to locate the position of the first occurrence of the substring “/” within the string. We then use the substr function to extract the characters that follow the “/” character. We add 1 to the position returned by strpos to exclude the “/” character itself from the extracted substring. Finally, we use the extracted substring to output the date.
Wrapping Up
In summary, checking if a string contains a specific pattern is a frequent task in PHP programming. Consequently, there are multiple approaches available to accomplish this in PHP, such as utilizing the strpos
and strstr
functions, employing regular expressions with the preg_match
function, and utilizing the in_array function in combination with the str_split
function.
Each method offers its own set of advantages and disadvantages, and the selection of the appropriate method depends on the specific requirements of the task.
However, when using the strpos
and strstr
functions, it is important to keep in mind that they are case-sensitive by default. If you need to perform a case-insensitive search, you can use the stripos
and stristr
functions instead.
Regular expressions provide a powerful and flexible way to search for patterns in strings. However, they can be complex and difficult to read, especially for beginners. If you are not familiar with regular expressions, it may be helpful to study the basics of regular expressions before attempting to use them in PHP.
The in_array
function with the str_split
function can be useful in situations where you need to check if a substring exists in a string character by character. However, it can be less efficient than the other methods, especially for long strings.
Overall, understanding how to perform string contains operation in PHP is an important skill for any PHP developer. With the methods described in this article, you should be able to perform this operation efficiently and effectively in your PHP programs.
To learn more visit PHP manual