PHP String Contains

PHP String Contains (5 Methods)

Used before category names. Blog PHP

In this PHP tutorial, we are going to check if a string is inside another string or not (find string in 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.

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 contains 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() 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 search
  • substring – Required, the substring that you are looking to check if the PHP string contains

Example of 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, it 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";
}

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(): aimed at detecting the position of a string’s last occurrence inside another string.
  • stripos(): aimed at detecting the position of a string’s first occurrence inside another string.
  • strripos(): aimed at detecting the position of a string’s last occurrence inside another string.
//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 search
  • substring – Required, the substring that you are looking to check if the string contains
  • start – 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 strops() 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 by Regular expression

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.

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.