The strchr() function, an alias of the strstr() function, searches for the first occurrence of a string inside another string.
The strchr() function is case-sensitive. For a case-insensitive search, use stristr() function.
PHP strchr() Function has the following syntax.
strchr(string,search,before_search);
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to search |
search | Required. | String to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number |
before_search | Optional. | Default to "false". If set to "true", it returns the part of the string before the first occurrence of the search parameter. |
PHP strchr() Function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.
Find the first occurrence of "world" inside "Hello world!" and return the rest of the string:
<?php
echo strchr("Hello world from java2s.com!","world");
?>
The code above generates the following result.
Search a string for the ASCII value of "o" and return the rest of the string:
<?php
echo strchr("Hello world!",111);
?>
The code above generates the following result.
Return the part of the string before the first occurence of "world":
<?php
echo strchr("Hello world!","world",true);
?>
The code above generates the following result.