PHP strstr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strstr() finds the first occurrence of a string and returns the part of the string from there to the end.

Set the third argument to true to get the text before the match instead. It returns false when nothing is found.

Syntax

Call strstr() with the values you need. Start with this line.

PHP
$domain = strstr($email, "@");
CallWhat it does
strstr("[email protected]", "@")Returns "@example.com".
strstr("[email protected]", "@", true)Returns "user", the part before the match.
strstr("hello", "z")Returns false because "z" is not found.

strstr() is case-sensitive. Use stristr() for a case-insensitive search. To only check if a string exists, use str_contains().

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

Parameters

ParameterDescription
haystackRequired. The string to search in.
needleRequired. The text to look for. Since PHP 8 it is always treated as a string.
before_needleOptional. If true, returns the part before the first match. Default is false.

Return Value

Return ValueDescription
stringThe part of haystack from the first match to the end, or the part before it when before_needle is true.
falseReturned when needle is not found.

PHP Examples

Example 1 — Split an Email Address

Get the part after and before the @ sign.

PHP
<?php
$email = "[email protected]";

echo strstr($email, "@"), PHP_EOL;
echo strstr($email, "@", true), PHP_EOL;

How It Works

  1. Search for "@". strstr() finds the first match.
  2. Default result. It returns everything from @ to the end.
  3. Use before_needle. Passing true flips the result.
  4. Get the username. The second call returns "user".

Example 2 — Not Found and Case Sensitivity

When there is no match, strstr() returns false. Letter case matters.

PHP
<?php
var_dump(strstr("hello", "z"));
var_dump(strstr("Hello", "h"));
echo stristr("Hello", "h"), PHP_EOL;

How It Works

  1. Missing text. "z" is not in "hello", so the result is false.
  2. Wrong case. "h" does not match capital "H".
  3. Use stristr(). It ignores case, so it finds the match.
  4. Check results. Always test for false before using the value.

Example 3 — Get a File Extension

Find everything from the first dot onward.

PHP
<?php
$file = "archive.tar.gz";

echo strstr($file, "."), PHP_EOL;
echo strstr($file, ".", true), PHP_EOL;

How It Works

  1. First dot only. strstr() stops at the first . it finds.
  2. Full extension. The first call returns .tar.gz.
  3. Base name. With true, you get archive.
  4. Tip. Use strrchr() when you want the last dot instead.

Frequently Asked Questions

strstr() looks for the first occurrence of a substring and returns the part of the string starting at that match. With before_needle set to true, it returns the part before the match.
string|false strstr(string $haystack, string $needle, bool $before_needle = false). The third argument is optional.
Yes. strstr("Hello", "h") returns false. Use stristr() for a case-insensitive version.
It returns false. Check with === false before using the result as a string.
strstr() matches the first occurrence of a whole string. strrchr() matches the last occurrence of a single character and returns the text from there.

Next: strtok()

strtok() splits a string into tokens (pieces) one at a time using a set of delimiter chara.

strtok() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful