menu

The stristr Function

The stristr function searches for the first occurrence of a substring in a string case-insensitively and returns the part of the string from that occurrence to the end. If the substring is not found, it returns false.

Syntax

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

Example

Case-insensitive substring search:

<?php $email = 'USER@EXAMPLE.com'; echo stristr($email, 'e'); // Finds the first 'E' ?>

Code execution result:

"ER@EXAMPLE.com"

Example

Using the third parameter to get the part before the found substring:

<?php $string = 'Hello World'; echo stristr($string, 'w', true); // Returns the part before 'W' ?>

Code execution result:

"Hello "

Example

Checking for the presence of a substring:

<?php $res = stristr('Hello World', 'xyz'); var_dump($res); ?>

Code execution result:

false

See Also

  • the strstr function,
    which performs a case-sensitive search
  • the stripos function,
    which returns the position of a substring case-insensitively
byenru