Which function is used to find the substring of string in php?

Sometimes it requires to check a particular character or string exists in another string. PHP has many functions to check a string contains another substring. Some of these PHP functions are case-sensitive and some are case-insensitive. The uses of two types of functions have been explained in this tutorial with examples.

Pre-requisite:

The scripts used in the examples of this tutorial are written based on the PHP 8 version. Do the following task before executing the examples of this script.

  1. Install apache2 and PHP 8.
  2. Set execute permission for all files and folders under /var/www/html folder where all PHP files will be stored.

Use of strpos() Function

The strpos() function is used to find out the position of the first occurrence of the substring in the main string. The syntax of this function is given below.

strpos(string $main_string, string $search_string, [,int $offset]):int | False

The first argument and the second argument of this function are mandatory arguments and the third argument is optional. The first argument takes the main string value and the second argument takes the search string value. The third argument takes the starting position of the argument. It returns the position value if the substring exists in the main string otherwise returns False.

Example-1: Search substring using strpos() function

Create a PHP file with the following script to search a substring in a string from the starting of the string and the particular position of the string. Here, the search string or substring and the starting position to start searching will be given the URL. The $_GET[] array has been used to read the value of the URL query parameters named ‘search’ and ‘pos’.

  //Define a string value

$string = "Welcome to LinuxHint.";

//Check search string has given in the URL or not

if(isset($_GET['search']))

{

    $search_str = $_GET['search'];

    //Check position value has given in the URL or not

   if(isset($_GET['pos']))

   {

        $position = $_GET['pos'];

        //Find the position of the search string after particular position

        if (strpos($string, $search_str, $position) !== False)

           echo "The $search_str found at position ".strpos($string, $search_str, $position);

        else

                   echo "The string doesn't contain the string.";

        }

        //Find the position of the search string

        elseif (strpos($string, $search_str) !== False)

        {

                  echo "The $search_str found at position ".strpos($string, $search_str);

          }

        else

                  //Print message if the position of the search word is not found

                  echo "The string doesn't contain the string.";

}

else

                  //Print message if no searching string has been given in the URL

                  echo "No searching string found."

?>

Output:

The following output will appear after executing the above script without any query parameter. Here, the filename is substr1.php that is stored inside /var/www/html/code folder.

http://localhost/code/substr1.php

Which function is used to find the substring of string in php?

The following output will appear after executing the above script with the query parameter named ‘search’.

http://localhost/code/substr1.php?search=Linux

Which function is used to find the substring of string in php?

The following output will appear after executing the above script with the query parameters named ‘search’ and ‘pos’.

http://localhost/code/substr1.php?search=com&pos=2

Which function is used to find the substring of string in php?

The following output will appear after executing the above script with the query parameters named ‘search’ and ‘pos’.

http://localhost/code/substr1.php?search=com&pos=5

Which function is used to find the substring of string in php?

Use of strstr() Function

The strstr() function is used to search a substring into a string and returns the string starts from the position of the substring to the end of the main string if the substring exists in the main string. The syntax of the strstr() function is given below.

strstr(string main_string, string search_string, [,bool $before_search = false]): string|false

The first argument and the second argument of this function are mandatory arguments and the third argument is optional. The first argument takes the main string value and the second argument takes the substring value. The default value of the third argument is False. If the third argument is set to True, then the function will return a string starting from the beginning of the main string to the position where the substring is found.

Example-2: Search substring using strstr() function

Create a PHP file with the following script to search a substring in a string using strstr() function. Here, the substring will be given the URL. The $_GET[] array has been used to read the value of the URL query parameter named ‘search’.

//Define a string value
$main_str = "PHP is a server-side scripting language.";
//Check search string has given in the URL or not
if(isset($_GET['search']))
{
    $search_str = $_GET['search'];
    //Find the search string exists in the main string or not
    if(strstr($main_str, $search_str) != false)
    {
        $output = strstr($main_str, $search_str);
        echo "The main string contains the search word, $search_str.
"
;
        echo "Search output: $output";
    }
}
else
    //Print message if no searching string has been given in the URL
    echo "No searching string found."

?>

Output:

The following output will appear after executing the above script with the query parameter named ‘search’ with the value ‘server’. Here, the filename is substr2.php that is stored inside /var/www/html/code folder.

http://localhost/code/substr2.php?search=server

Which function is used to find the substring of string in php?

Use of str_contains() Function

The str_contains() function exists in PHP 8 version only. It is used to check a substring exists in the main string or not. The syntax of this function is given below.

str_contains(string $main_string, string $search_string): bool

The first argument and the second argument of this function are mandatory arguments. The first argument takes the main string value and the second argument takes the search string value. It returns True if the substring exists in the main string otherwise returns False.

Example-3: Search substring using str_contains() function

Create a PHP file with the following script to search a substring in a string using the str_contains() function. Here, the substring will be given the URL. The $_GET[] array has been used to read the value of the URL query parameter named ‘search’.

//Define a string value
$string = 'HTML is a mark-up language.';
//Check search string has given in the URL or not
if(isset($_GET['search']))
{
    $search_str = $_GET['search'];
    echo "The main string:
$string
"
;
    echo "The search string:
$search_str
"
;
    //Check the search string exists in the main string or not
    if(str_contains($string, $search_str))
        echo "
The search string exist in the main string"
;
    else
        echo "
The search string doesn't exist in the main string."
;
}

?>

Output:

The following output will appear after executing the above script with the query parameter named ‘search’ with the value ‘language’. Here, the filename is substr3.php that is stored inside /var/www/html/code folder.

http://localhost/code/substr3.php?search=language

Which function is used to find the substring of string in php?

The following output will appear after executing the above script with the query parameter named ‘search’ with the value ‘script’.

http://localhost/code/substr3.php?search=script

Which function is used to find the substring of string in php?

Conclusion

The uses of three built-in functions have been described in this tutorial to check a substring exists in another string or not. I hope the PHP users will be able to use any of the functions mentioned here to search the substring based on their requirements after reading this tutorial.

About the author

Which function is used to find the substring of string in php?

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

What is the use of substr () function in PHP?

What is substr in PHP? substr in PHP is a built-in function used to extract a part of the given string. The function returns the substring specified by the start and length parameter. It is supported by PHP 4 and above.

What is the use of substr () in string?

The substring() method extracts characters from start to end (exclusive). The substring() method does not change the original string. If start is greater than end, arguments are swapped: (4, 1) = (1, 4). Start or end values less than 0, are treated as 0.

How do you check a string contains a substring in PHP?

Answer: Use the PHP strpos() Function You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

What is the use of strpos () function in PHP?

strpos in PHP is a built-in function. Its use is to find the first occurrence of a substring in a string or a string inside another string. The function returns an integer value which is the index of the first occurrence of the string.