PHP strchr()Functions
Examples
Find the first occurrence of "world" in "Hello world!" and return the rest of the string:
<?php
echo strchr("Hello world!","world");
?>
echo strchr("Hello world!","world");
?>
Run example »
Definition and Usage
The strchr() function searches for the first occurrence of a string within another string.
This function is strstr()an alias of the function.
Note:This function is binary-safe.
Note:This function is case-sensitive. For a case-insensitive search, use thestristr()function.
Syntax
strchr(string,search,before_search);
| Parameter | Description |
|---|---|
| string | Required. Specifies the string to be searched. |
| search | Required. Specifies the string to search for. If this parameter is a number, it searches for the character matching the ASCII value of that number. |
| before_search | Optional. A boolean value with a default of "false". If set to "true", it returnssearchthe part of the string before the first occurrence of the parameter. |
Technical Details
| Return value: | Returns the rest of the string (from the match point). If the searched string is not found, it returns FALSE. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | In PHP 5.3, thebefore_searchparameter was added. |
More Examples
Example 1
Search for the string by the ASCII value of "o" and return the rest of the string:
<?php
echo strchr("Hello world!",111);
?>
echo strchr("Hello world!",111);
?>
Run example »
Example 2
Return the part of the string before the first occurrence of "world":
<?php
echo strchr("Hello world!","world",true);
?>
echo strchr("Hello world!","world",true);
?>
Run example »
PHP String Reference Manual Other Extensions