When it comes to working with strings in PHP, one fundamental task is checking whether a string contains another str, string, and substring or not like with the str_contains function. In this PHP tutorial, we are going to check if a string is inside another string or not (find the string(str) in a string). There are various functions and methods to check the string contains substrings in PHP, which we will discuss step by step in this tutorial.
In this article, we will explore the various methods available in PHP to determine if a string(str) contains a specific substring. We will cover both basic and advanced techniques.
By the end, you’ll have a thorough understanding of how to handle string containment effectively, enabling you to write robust and efficient PHP applications.
Why do we need to find a string inside another string?
Sometimes we need to check if a string is inside another string or not. For example, we want to search for a word in the text. For this, we need PHP string(str) contains and substring and its methods.
The most common way to determine if the string contains a substring is to use the str_contains()
function, but in addition to this function, we will discuss other functions and methods.
By using the methods presented in this post, in addition to checking the existence of a sub-string in the main string, we can also determine the placement index of the sub-string.
1- str_contains() PHP built-in function
The str_contains function is provided in PHP version 8 and determines if a string contains a given substring.
str_contains(string, substring)
string
– Required, the original string that you intend to searchsubstring
– Required, the substring that you are looking to check if the PHP string contains
Example of the str_contains function
$word = "text";
$string = "This is a test text to PHP string contains.";
if (str_contains($string, $word)) {
echo "The word found";
} else {
echo "The word not found";
}
Output
The word found
If the string contains a substring, the str_contains function returns true, otherwise, it returns false. One thing to keep in mind is that this function is case-sensitive. To determine whether the string contains a substring or not, you must first convert both texts (string and substring) to lowercase letters and then perform the search operation.
$word = strtolower("Text");
$string = strtolower("This is a test text to PHP string contains.");
if (str_contains($string, $word)) {
echo "The word found";
} else {
echo "The word not found";
}
In this example, the str_contains function returns true because the letter “text” is in the sentence.
2- strops() function
The strops()
function in PHP not only determines the existence of a substring within the main string but also the location of the first occurrence of the substring. If the entered substring does not exist inside the main string, it returns false, and if it does, it returns its index. This function is considered case-sensitive and binary-safe.
strrpos()
: Thestrrpos()
function is similar tostrpos()
, but it searches for the last occurrence of a substring within a string. This function can be useful when you need to find the position of the last occurrence rather than the first.stripos()
andstrripos()
: In situations where case sensitivity is not essential, PHP provides thestripos()
andstrripos()
functions. These functions are similar tostrpos()
andstrrpos()
, respectively, but they perform case-insensitive searches.
//Syntax of stripos
stripos(string, substring, start)
//Syntax of strpos
strpos(string, substring, start)
//Syntax of strrpos
strrpos(string, substring, start)
string
– Required, the original string that you are looking to searchsubstring
– Required, the substring that you are looking to check if the string containsstart
– Optional, specific where the search for the substring should begin
Note: Return index in this function starts from 0 like arrays in PHP.
$word = "text";
$string = "This is a test text to PHP string contains.";
$index = strpos($string, $word);
if($index !== false){
echo "The word found in $index";
}
else{
echo "The word not found";
}
Output
The word found in 15
The strpos()
function is case-sensitive. To avoid this, we must either convert the term to lowercase before searching or use the stripos()
function.
$word = "Text";
$string = "This is a test text to PHP string contains.";
$index = stripos($string, $word);
if($index !== false){
echo "The word found in $index";
}
else{
echo "The word not found";
}
The drawback of this function is that if the search term is at the beginning of the string, the return value of the function will be false, while the substring is inside the string. Therefore, it is better to use the strops()
function and convert the expressions (string and substring) to lowercase letters before checking.
3- Check PHP string contains Regular expression
When dealing with complex string patterns, regular expressions provide a powerful solution. By using regular expressions in PHP, it is possible to determine whether the string contains a substring or not. Using this method, you can have more control over how to search, but its speed is lower than the above methods.
$string = "This is a test text to PHP string contains.";
if (preg_match("/text/i", $string)) {
echo "The word found";
} else {
echo "The word not found";
}
Output
The word found
By using regular expressions, you can easily have more control over the search term. This example is not case-sensitive.
Regular expressions can be used in many cases, for example, they can be used to validate form inputs that we have used here for searching.
4- Using PHP iteration loops to detect the string containing the substring
Strings in PHP are actually an array of characters that we use this method to extract and check the substring inside a string. So that we read the string character by character and match it with the search term. If there is a match, the string contains the substring.
It is also possible to use repetition loops in PHP for this task, but despite the previous methods, we do not recommend it, but for your familiarity, we will also explain this method in this section.
In this example, we only use PHP’s for loop to detect the existence of a substring inside the string.
$word = 'text';
$string = 'This is a test text to PHP string contains.';
$contains = false;
$index = -1;
for ($counter = 0; $counter < strlen($string); $counter++) {
if ($string[$counter] === $word[0]) {
if (substr($string, $counter, strlen($word)) === $word) {
$index = $counter;
$contains = true;
break;
}
}
}
if ($contains) {
echo "The word found in $index";
} else {
echo 'The word not found';
}
Output
The word found in 15
5- Using arrays for PHP contains the string
In this method, we use arrays and function explode() and function in_array() which are built-in functions in PHP to determine if there is a substring inside the text or not.
$word = 'text';
$string = 'This is a test text to PHP string contains.';
$arr_str = explode(' ', $string);
if (in_array($word, $arr_str)) {
echo 'The word found';
} else {
echo 'The word not found';
}
Output
The word found
In this example, we search for text within the text. The problem in this example is that the text words are separated by spaces. For example, if there is .text inside the text, this method cannot work correctly.
To solve this problem, the example code can be changed as follows so that it does not depend on space.
$word = 'text';
$string = 'This is a test text to PHP string contains.';
$arr_str = explode($word, $string);
if (count($arr_str) > 1) {
echo 'The word found';
} else {
echo 'The word not found';
}
In this example, the text is divided into several parts (more than one item) if the word text is present, and we can determine from there whether the text contains a substring or not.
6. substr_count() function
The substr_count()
function allows you to count the number of times a substring appears within a given string. It can be handy when you need to determine the frequency of a particular substring.
$haystack = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$needle = "ipsum";
$count = substr_count($haystack, $needle);
echo "The substring occurs " . $count . " times in the string.";
In this case, substr_count()
will return the count of occurrences of “ipsum” within the haystack
string, which is 1.
Conclusion
In this article, we introduced methods to search for substrings inside a string and to determine whether a string contains a substring or not. All the methods presented in this tutorial can be used for Unicode characters as well, and you can even use them to search within right-to-left (RTL) texts such as Arabic.