PHP strstr Function | Find First Occurrence of String

PHP strstr 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 strstr Function. Also, we will discuss a few examples of using it.

This function is case-sensitive. If you need a case-insensitive search, use PHP stristr Function. Also, the PHP strrchr Function finds the last occurrence of string in another string.

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.

Syntax

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

Parameters

The PHP strstr 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 strstr 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.

PHP strstr Function diagram

Examples

Let’s discuss a few examples of using the PHP strstr Function.

Example 1: Basic Search

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

<?php

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

    //Find Knowledge in the original string
    $partString = strstr($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.

OUTPUT:
Knowledge Together

Example 2: Set Before Needle Parameter to True

In the same example above, if we set the before_needle parameter to true, the strstr function in PHP will return the part of string before the first occurrence.

<?php

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

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

?>

The above code will print the following:

OUTPUT:
Bringing

Example 3: String Not Found

In case the needle is not present in the original string, the function returns false.

<?php

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

    //Find Concatly in the original string
    $partString = strstr($haystack, $needle);
    var_dump($partString);

?>
OUTPUT:
bool(false)

Example 4: Case Sensitive

Also, the PHP strstr Function is a case sensitive function. Therefore, it treats lowercase and uppercase characters differently.

<?php

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

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

?>
OUTPUT:
bool(false)

Conclusion

In conclusion, we discussed the PHP strstr 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 *