Javatpoint Logo
Javatpoint Logo

PHP String strstr() function

The strstr() function is an in-built function of PHP. It is a case-sensitive function which finds the first occurrence of a string. The strstr() is mainly used to search the first occurrence of a string inside another string and displays some part of the latter starting from the first occurrence of the former in latter.

It is a binary-safe function, means a function that can perform on binary files without modifying the content of the file. The strstr() function is similar to stristr() function, but the only difference is that - stristr() is case-insensitive function whereas strstr() is case-sensitive.

Note: The strstr() is binary-safe and case-sensitive function.


The syntax for the PHP strstr() function is given below, which consists three parameters.


$string (required): $string is a mandatory parameter which specifies the string to be searched. In other words, it is the main string parameter in which $search value is searched.

$search (required): The next mandatory parameter of this function is $search. It specifies that string which is going to be searched in $string parameter. If this parameter contains a number or integer value rather than a string, then it will search for character matching ASCII value for that number.

$before_search (optional): It is the last and optional parameter of strstr() function which specifies the Boolean value, whose default value is FALSE. If we set it to TRUE, then it will return the part of the string before the first occurrence of the search parameter.

Return Values

The PHP strstr() returns the remaining string (from the matching point), or it will return FALSE if the string which we are searching for is not found.

Technical Details

PHP version support PHP 4+ versions support this function.
Return Values The strstr() returns rest of the string or returns False if the string for search is not found.
  • In strstr(), $before_search parameter was added in PHP 5.3

Examples of strstr()

There are some examples given below that will help us to learn the practical use of this function.

//Returns remaining string after search found
$string1 = "Hello! Good Morning everyone", $search1 = "Good";
Output: Morning everyone

//case-sensitive, returns nothing
$string = "Hello! Good Morning everyone ", $search = "HELLO ";

//Returns initial string when search found
$string = "Hello! Good Morning everyone", $search = "Good", before_search = true;
Output: Hello!								

//Passing ASCII value of r which is 114
$string = "I want to travel the world ", $search = "114", before_search = true;
Output: I want to t

Below some detailed examples are given. With the help of these examples, we can understand the use of this function in a much better way.

Example 1

It is a simple example of strstr() which shows that it is a case-sensitive function, so it returns FALSE if the string does not match. It will return the rest of the string from where the $search variable will find in the string.


PHP String strstr() function

Example 2

In this below example, we will use a third parameter $before_search, whose default value is FALSE. Here, we will set it to TRUE, which will return the part of the string before the first occurrence of the $search parameter. If the search string is not found, then it will return the Boolean value false.


PHP String strstr() function

Example 3

We can also pass an integer value in $search parameter rather than the string or character. This integer value is treated as ASCII value and converted into character. In this example, we will pass an integer in $search.


PHP String strstr() function

Note: var_dump() function is mainly used to display the Boolean values on the browser. Other than the Boolean value, it displays the datatype of parameter and number of character available in that parameter, as you can see in the above example. Echo is not sufficient to print these information.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA