Javatpoint Logo
Javatpoint Logo

stristr() function in PHP

The stristr() is an in-built function of PHP which is used to search the first occurrence of the string inside another string, and it returns rest of the string if the string is present. It is a binary-safe function (Binary safe function means - a function which can be performed on binary file without modifying the content of the file).

The stristr() is a case-insensitive function which is similar to the strstr(). Both functions are used to search a string inside another string. The only difference between them is that stristr() is case-insensitive whereas strstr() is case-sensitive function.

Note: This function is binary-safe and case-insensitive function. In stristr() function 'I' stands for insensitive.


The syntax for the PHP stristr() function is as follows:


$string (required): This parameter is a mandatory parameter which specifies the string which is to be searched, means it is the main string in which $search (discussed next) value is searched.

$search (required): This parameter is also mandatory as $string. This parameter specifies the string which is going to be searched in $string. If this parameter is a number or integer value rather than a string, then it will use it as ASCII value.

$before_search (optional): This parameter is an optional parameter 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 stristr() returns the remaining string (starts 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 and above versions support this function.
Return Values It returns rest of the string or returns False if the string to be searched is not found.
  • In PHP 5.3, the before_search parameter was added in this function.
  • The stristr() function became a binary-safe function in PHP 4.3.


Below are some examples through which you can learn the practical implementation of the stristr() function in the program.

$string = "Hello PHP! ", $search = "PHP ";
Output: PHP!

$string = "Hello PHP! ", $search = "p ", before_search = true;		//case-insensitive
Output: Hello

$string = "Hello PHP! ", $search = "K ", before_search = true;

Following are some detailed examples which are given below -

Example 1

It is the simple example of stristr() which shows that it is case-insensitive function and return the rest of the string where the $search variable will find.


PHP String stristr() function

Example 2

In this example, we use the third parameter, i.e., $before_search = true, which will return the part of the string before the first occurrence of the $search1 variable otherwise Boolean value false.


We used var_dump() function to print the false value because echo was not sufficient to print this Boolean value as you can see in the output while searching $search1 value.

PHP String stristr() function

Example 3

In this example, we will pass an integer in $search rather than string or character. It treats that integer value as ASCII value.


PHP String stristr() function

Note: var_dump() function also print the number of character including whitespace and their return type. E.g. false is a Boolean value whereas in second line, number of character returned by the stristr() is a string. Echo does not display 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