PHP stristr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, stristr() finds the first occurrence of a substring and returns the rest of the string from that point, ignoring case.

It is the case-insensitive version of strstr(). Set the third argument to true to get the part before the match instead.

Syntax

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

PHP
$rest = stristr($email, "EXAMPLE");
CallWhat it does
stristr("[email protected]", "EXAMPLE")Returns "Example.COM".
stristr("[email protected]", "@", true)Returns "user" (the part before the match).
stristr("Hello", "xyz")Returns false because there is no match.

To only check whether a substring exists, use stripos() or str_contains() (PHP 8.0+). They are clearer and faster than comparing the result of stristr().

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

Parameters

ParameterDescription
haystackRequired. The string to search in.
needleRequired. The text to look for. Case is ignored.
before_needleOptional. If true, returns the part before the first match. Default is false.

Return Value

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

PHP Examples

Example 1 — Find the Rest of a String

Search without worrying about upper or lower case.

PHP
<?php
$email = "[email protected]";
$rest = stristr($email, "EXAMPLE");

echo $rest, PHP_EOL;

How It Works

  1. Store an email address with mixed case in the domain.
  2. Search for "EXAMPLE" in uppercase.
  3. stristr ignores case and finds the match.
  4. It returns everything from the match to the end, with the original case kept.

Example 2 — Get the Part Before the Match

Set the third argument to true to get the text before the needle.

PHP
<?php
$email = "[email protected]";
$name = stristr($email, "@", true);

echo $name, PHP_EOL;

How It Works

  1. Use an email address as the haystack.
  2. Search for "@" and pass true as before_needle.
  3. PHP returns the text before the first "@".
  4. This is a quick way to get the user name part.

Example 3 — Handle a Missing Match

When the needle is not found, stristr() returns false.

PHP
<?php
$result = stristr("Hello", "xyz");

var_dump($result);

if ($result === false) {
    echo "Not found", PHP_EOL;
}

How It Works

  1. Search for text that does not exist in the string.
  2. stristr returns false instead of an empty string.
  3. var_dump shows the exact type: bool(false).
  4. Use === false to test for the missing match safely.

Frequently Asked Questions

stristr() finds the first occurrence of a needle in a string, ignoring case, and returns the rest of the string from that match. It returns false if nothing is found.
strstr() is case-sensitive. stristr() is case-insensitive. Both return the portion of the string that starts at the first match.
Pass true as the third argument: stristr($haystack, $needle, true). PHP then returns the part before the first match.
It returns false. Check with === false because other return values can be falsy strings.
No. For a simple yes or no check, use str_contains() (PHP 8.0+) or stripos(). They are easier to read and designed for that job.

Next: strlen()

strlen() returns the length of a string, counted in bytes.

strlen() 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