Facebook Page Twitter Page LinkedIn Page
× PHP Tutorial PHP Advanced PHP Resources


The PHP stristr() function is used to return a string which starts from first occurrence of the specified text to the end of the given string.

The function has one optional parameter. The deafult value of the parameter is false or 0. In case of "true" or 1, the function returns string from start of the given string to the first occurrence of the specified string.

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

Note: The stristr() function is a binary-safe but not a case-sensitive 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. If number is provided instead of string, then it searches for first occurrence of the character having ASCII value matching with the number.
before_search Optional. Specify "true/false" or "1/0". Default is "false". In case of "true", it returns string from start of the given string to the first occurrence of the specified string.

Return Value

Returns a string which starts from first occurrence of the specified text to the end of the given string if before_search is either false or 0.
Returns a string which starts from the start of the given string to the first occurrence of the specified string if before_search is either true or 1.

Example:

In the below example, stristr() function to used to return the part the string called MyString using search string "not".

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

  $NewString = stristr($MyString, "NOT");
  echo $NewString."\n";

  $NewString = stristr($MyString, "NOT", 1);
  echo $NewString."\n";
?>

The output of the above code will be:

not to be, that is the question

To be, or 

❮ PHP String functions