PHP strchr() Function
Topic: PHP String ReferencePrev|Next
Description
The strchr()
function find the first occurrence of a string within another string.
This function is case-sensitive. Also, this function is an alias of strstr()
.
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 strchr()
function is given with:
The following example shows the strchr()
function in action.
Example
Run this code »<?php
// Sample string
$str = "[email protected]";
// Searching for the substring
echo strchr($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 strchr()
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 strchr()
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 strchr($str, "@", true);
?>