PHP strstr() Function

Usage — The PHP strstr() function is used to find the first occurrence of a substring inside another string.

It has the following syntax:

PHP

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

Here is an example of using strstr():

PHP

// Output — ", Adam"
echo strstr("Hello, Adam",", ");

Return Value — This function returns the portion of haystack string starting from and including the first occurrence of the needle to the end of haystack. It will return FALSE if needle is not found.

Additional Information — This function is case-sensitive and binary-safe.

If you only want to determine if a particular needle occurs within haystack, you can use the faster and less memory intensive function strpos() instead.

PHP Version and Changelog — The strstr() function is available in PHP 4, PHP 5, PHP 7. It was made binary-safe in PHP version 4.3.0 and the optional before_needle parameter was added to it in PHP version 5.3.0.

Relevant Functions — Other related PHP functions that you should know about are: stristr() which is case-insensitive version of strstr(), strrchr() which finds the last occurrence of a character in a string, strpos() which finds the position of the first occurrence of a substring in a string, 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 input string. This is a required parameter.

needle

The needle parameter is used to specify the string that you want to 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 a required parameter.

before_needle

The before_needle parameter is a boolean value which determines that part of haystack that should be returned. If TRUE, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle). This is an optional parameter and its default is FALSE.

Working Examples

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

PHP

$email  = 'name@example.com';

$domain = strstr($email, '@');
// Output — @example.com
echo $domain;

$user = strstr($email, '@', true);
// Output — name (as of PHP 5.3.0)
echo $user;

Additional Tips

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

  1. If $needle is the last character in $haystack, and testing $needle as a boolean by itself would evaluate to false, then testing strstr() as a boolean will evaluate to false (because, if successful, strstr() returns the first occurrence of $needle along with the rest of $haystack).

    PHP

    findZero('01234');  // found a zero
    findZero('43210');  // did not find a zero
    findZero('0');      // did not find a zero
    findZero('00');     // found a zero
    findZero('000');    // found a zero
    findZero('10');     // did not find a zero
    findZero('100');    // found a zero
    function findZero($numberString) {
       if (strstr($numberString, '0')) {
            echo 'found a zero';
        } else {
            echo 'did not find a zero';
        }
    }
    

    Also, strstr() is far more memory-intensive than strpos(), especially with longer strings as your $haystack, so if you are not interested in the substring that strstr() returns, you shouldn’t be using it anyway.

    There is no PHP function just to check only if $needle occurs in $haystack. The function strpos() tells you if it doesn’t by returning false, but, if it does occur, it tells you where it occurs as an integer, which is 0 (zero) if $needle is the first part of $haystack, which is why testing if (strpos($needle, $haystack)===false) is the only way to know for sure if $needle is not part of $haystack.

    My advice is to start loving type checking immediately, and to familiarize yourself with the return value of the functions you are using.

    Suggested by - laszlo dot heredy at gmail dot com

  2. For the needle_before (first occurance) parameter when using PHP 5.x or less, try:

    PHP

    $haystack = 'php-homepage-20071125.png';
    $needle = '-';
    // Output — php
    echo substr($haystack, 0, strpos($haystack, $needle));
    

    Suggested by - brett dot jr dot alton at gmail dot com

  3. If you want to emulate strstr()’s new before_needle parameter pre 5.3, strtok() is faster than using strpos() to find the needle and cutting with substr(). The amount of difference varies with string size but strtok() is always faster.

    Suggested by - w3b_monk3y at yahoo dot com

Further Reading

  1. You can read more about the PHP strstr() 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