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


The PHP String strstr() function returns 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 default 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 stristr() function, except stristr() function is not case-sensitive function.

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

Syntax

strstr(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, strstr() function returns the part the string called MyString using search string "not".

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

$NewString = strstr($MyString, "not");
echo $NewString."\n";

$NewString = strstr($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