PHP stristr Function | Find First Occurrence of String

PHP stristr is an inbuilt function in PHP. It finds the first occurrence of a string in another string. Also, it returns the rest of the string after the first occurrence. In this article, we will discuss the PHP stristr Function. Also, we will discuss a few examples of using it.

This function is case-insensitive. Uppercase and Lowercase characters are treated as same. If you need a case-sensitive search, use PHP strstr Function.

Note: If you need to just check whether a string exists in another string, with faster and less memory intensive function, use PHP strpos Function.


stristr($haystack, $needle, $before_needle = false)


The PHP stristr Function expects three parameters. However, two parameters are mandatory and one is optional. The description of the parameters is as follows:

  • $haystack: The first parameter to the function is the string in which you want to search in. It is a mandatory parameter.
  • $needle: The second parameter to the function is also a string which you want to search. If this parameter is an integer then it will search for the character matching the ASCII value of the number. However, this behavior is depreciated as of PHP 7.3. Therefore, either the needle should be explicitly cast to string or a call to PHP chr function should be performed.
  • $before_needle: If the before_needle is set to true then the function returns the part of the string before the needle. However, it is an optional parameter with the default value as false. Hence, the function returns the part of the string after the first occurrence of needle.

Return Value

The PHP stristr Function returns the part of the string according to before_needle parameter. However, if the search string does not exist in the original string, the function returns FALSE. The function is case-insensitive.

PHP stristr Function

Example 1: Basic Search

For instance, consider a basic haystack and a needle string.


    $haystack = 'Bringing Knowledge Together';
    $needle = 'Knowledge';

    //Find Knowledge in the original string
    $partString = stristr($haystack, $needle);
    echo $partString;


In the above example, we search for ‘Knowledge’ in the original string. The function returns the part of the string after the first occurrence of the string.

Knowledge Together

Example 2: Case Insensitive

Also, the PHP stristr Function is a case insensitive function. Therefore, it treats lowercase and uppercase characters as same.


    $haystack = 'Bringing Knowledge Together';
    $needle = 'KNOWLEDGE';

    //Find KNOWLEDGE in the original string
    $partString = stristr($haystack, $needle);

Knowledge Together

For further detailed examples, you can refer to PHP strstr Function.


In conclusion, we discussed the PHP stristr Function in this article. You can read more about it on PHP Official Documentation. Additionally, you can learn about other PHP String Functions on Concatly!

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *