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

stristr

PHP function Edit on GitHub ✎

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

Case-insensitive strstr

Description

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

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

Parameters

haystack

The string to search in

needle

The string to search for.

Non-string

before_needle

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

needle and haystack are examined in a case-insensitive manner.

Return Values

Returns the matched substring. If needle is not found, returns false.

Changelog

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

Examples

stristr() example

php
<?php
  $email = 'USER@EXAMPLE.com';
  echo stristr($email, 'e'), PHP_EOL; // outputs ER@EXAMPLE.com
  echo stristr($email, 'e', true), PHP_EOL; // outputs US
?>

Testing if a string is found or not

php
<?php
  $string = 'Hello World!';
  if (stristr($string, 'earth') === FALSE) {
    echo '"earth" not found in string';
  }
// outputs: "earth" not found in string
?>

Notes Bin-safe

See Also

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