PHP Tutorial - PHP strchr() Function






Definition

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.

Syntax

PHP strchr() Function has the following syntax.

strchr(string,search,before_search);

Parameter

ParameterIs RequiredDescription
stringRequired.String to search
searchRequired.String to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number
before_searchOptional.Default to "false". If set to "true", it returns the part of the string before the first occurrence of the search parameter.




Return

PHP strchr() Function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.

Example 1

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.

Example 2

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.





Example 3

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.