PHP stristr() Function

Usage — The PHP stristr() function is used to search for the first occurrence of a string inside another string in a case-insensitive manner.

It has the following syntax:

PHP

string stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

Here is an example of using stristr():

PHP

// Output — Adam!
echo stristr("Hello Adam!","ADAM");

Return Value — This function returns all of the haystack starting from and including the first occurrence of the needle to the end.

Additional Information — This function is binary-safe and case-insensitive. If you want to perform a case-sensitive search, you should use the strstr() function instead.

PHP Version and Changelog — The stristr() function is available in PHP 4, PHP 5, PHP 7. The function became binary safe in PHP version 4.3.0 and the optional before_needle parameter was added in PHP version 5.3.0.

Relevant Functions — Other related PHP functions that you should know about are: strstr() which finds the first occurrence of a string, strrchr() which finds the last occurrence of a character in a string, stripos() which finds the position of the first occurrence of a case, strpbrk() which searches a string for any of a set of characters and preg_match which performs a regular expression match.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

haystack

The haystack parameter is used to specify the string that the function should search in. This is a required parameter.

needle

The needle parameter is used to specify the string that the function should search for. If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. This is also a required parameter.

before_needle

The before_needle parameter is a boolean which is used to specify if the function should return the part of haystack that comes before or after the needle. It is an optional parameter and its default value is FALSE. When set to TRUE, stristr() returns the part of the haystack before the first occurrence of the needle (excluding the needle).

Working Examples

Here are some examples of using the stristr() function:

PHP

$email = 'USER@EXAMPLE.com';
// Output — ER@EXAMPLE.com
echo stristr($email, 'e');

// Output — US (As of PHP 5.3.0)
echo stristr($email, 'e', true);

Testing if a string is found or not

PHP

$string = 'Hello World!';

// Output — "earth" not found in string
if(stristr($string, 'earth') === FALSE) {
  echo '"earth" not found in string';
}

Using a non “string” needle

PHP

$string = 'APPLE';

// Output — APPLE (because 97 is lowercase a)
echo stristr($string, 97);

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. There was a change in PHP 4.2.3 that can cause a warning message to be generated when using stristr(), even though no message was generated in older versions of PHP.

    The following will generate a warning message in 4.0.6 and 4.2.3: stristr("haystack", ""); or $needle = ""; stristr("haystack", $needle);.

    This will not generate an “Empty Delimiter” warning message in 4.0.6, but will in 4.2.3: unset($needle); stristr("haystack", $needle);.

    Suggested by - dpatton.at.confluence.org

  2. An example for the stristr() function:

    PHP

    $a = "I like php";
    
    // Output — According to I like php, you like PHP.
    if (stristr("$a", "LikE PhP")) {
        print ("According to \$a, you like PHP.");
    }
    

    It will look in $a for "like php" (NOT case-sensitive. though, strstr() is case-sensetive).

    Suggested by - Techdeck at Techdeck dot org

Further Reading

  1. You can read more about the PHP stristr() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top