PHP strstr() Function
Topic: PHP String ReferencePrev|Next
Description
The strstr()
function find the first occurrence of a string within another string.
This function is case-sensitive. For case-insensitive searches, use the stristr()
function.
The following table summarizes the technical details of this function.
Return Value: | Returns the portion of string, or FALSE if the string to search for is not found. |
---|---|
Changelog: | Since PHP 7.3.0, passing an integer as search parameter has been deprecated. |
Version: | PHP 4+ |
Syntax
The basic syntax of the strstr()
function is given with:
The following example shows the strstr()
function in action.
Example
Run this code »<?php
// Sample string
$str = "[email protected]";
// Searching for the substring
echo strstr($str, "@");
?>
Tip: If you simply want to find out if a particular substring occurs within a string or not, use the faster and less memory intensive function strpos()
instead.
Parameters
The strstr()
function accepts the following parameters.
Parameter | Description |
---|---|
string | Required. Specifies the string to search. |
search | Required. Specifies the string to search for. |
before_search | Optional. If set to true , it returns the part of the string before the first occurrence of the search string. Default value is false which returns all of the string after the first occurrence of the search string (including search string itself). |
More Examples
Here're some more examples showing how strstr()
function actually works:
The following example returns the part of the string before the first occurrence of @ symbol.
Example
Run this code »<?php
// Sample string
$str = "[email protected]";
// Searching for the substring
echo strstr($str, "@", true);
?>