php8.5
Home/ Manual/ strings / functions/ strstr

strstr

PHP function Edit on GitHub ✎

(PHP 4, PHP 5, PHP 7, PHP 8)

Find the first occurrence of a string

Description

strstr(string $haystack, string $needle, bool $before_needle = false): string|false

Returns part of haystack string starting from and including the first occurrence of needle to the end of haystack.

Note

This function is case-sensitive. For case-insensitive searches, use stristr().

Note

If it is only required to determine if a particular needle occurs within haystack, the faster and less memory intensive str_contains() function should be used instead.

Parameters

haystack

The input string.

needle

The string to search for.

Non-string

before_needle

If true, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle).

Return Values

Returns the portion of string, or false if needle is not found.

Changelog

VersionDescription
8.0.0Passing an Integer as needle is no longer supported.
7.3.0Passing an Integer as needle has been deprecated.

Examples

strstr() example

php
<?php
$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain, PHP_EOL; // prints @example.com

$user = strstr($email, '@', true);
echo $user, PHP_EOL; // prints name
?>

See Also

Source: reference/strings/functions/strstr.xml · from the official PHP manual (php/doc-en)