PHP 8 reference · String Functions

PHP strstr() Function

Return the part of a string from the first occurrence of a substring to the end, or the part before it.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
haystack The string to search in.
needle The string to find.
before_needle optional true returns the part before the match instead. Default: false

Return value

The matching part, or false if the needle isn't found.

Examples

Split around a characterTry it yourself
<?php
$email = "[email protected]";
echo strstr($email, "@"), "\n";
echo strstr($email, "@", true), "\n";

Output

@example.com
ada
No matchTry it yourself
<?php
var_dump(strstr("no-at-sign", "@"));

Output

bool(false)

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?