How To Find First Occurrence in String - strstr() and stristr() Function

PHP String Functions

The strstr() function and stristr() function can find the first occurrence of a substring (second parameter) inside another string (first parameter), and return all characters from the first occurrence till the end of the string.

strstr() function is case-sensitive and stristr() is case-insensitive.

Syntax

stristr(string, search, before_search)

strstr(string, search, before_search)

Parameters

  1. string (Required) - Where to search; Specifies the string to search
  2. search (Required) - What to search; Specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number
  3. before_search (Optional) - A boolean value whose default is "false". If set to "true", it returns the part of the string before the first occurrence of the search parameter

The before_search parameter was added in PHP 5.3 version.

Example

<?php
$email = 'USER@EXAMPLE.com';
echo stristr($email, 'e'); // outputs ER@EXAMPLE.com
echo stristr($email, 'e', true); // As of PHP 5.3.0, outputs US
?>
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive