PHP Function Reference

PHP stristr() Function



The PHP stristr() function returns a string which starts from first occurrence of the search string to the end of the given string.

The function has one optional parameter. The default value of this parameter is false. If this is set to true, the function returns string from start of the given string to the first occurrence of the search string.

This function is same as strstr() function, except this function is a case-insensitive function.

Note: The stristr() function is a binary-safe but a case-insensitive function.

Syntax

stristr(string, search, before_search)

Parameters

string Required. Specify the string to search.
search Required. Specify the string to search for in the given string.
Prior to PHP 8.0.0, if it is not a string, it is converted to an integer and applied as the ordinal value of a character. This behavior is deprecated as of PHP 7.3.0. Depending on the intended behavior, this should either be explicitly cast to string, or an explicit call to chr() should be performed.
before_search Optional. If set to true, the function returns string from start to the first occurrence of the specified text. Default is false.

Return Value

  • If the third parameter is set to false, it returns a string starting from first occurrence of the search string to the end of the given string.
  • If the third parameter is set to true, it returns a string starting from the start of the given string to the first occurrence of the search string in the given string.
  • Returns false if search string is not found.

Example:

The example below shows the usage of stristr() function.

<?php
$str = "To be, or not to be, that is the question";

//displaying a portion of $str starting from 
//first found "NOT" to the end of it
echo stristr($str, "NOT")."\n";

//displaying a portion of $str starting from 
//beginning of it to the first found "NOT"
echo stristr($str, "NOT", 1)."\n";
?>

The output of the above code will be:

not to be, that is the question
To be, or 

❮ PHP String Reference