PHP | strchr() Function
The strchr() function is a built-in function in PHP and is used to search for the first occurrence of a given string(say searchStr) in another string(say originalStr) and returns the rest of the string from originalStr starting from the first occurrence of searchStr in orignalStr.
Note: The strchr() function is case sensitive.
Syntax:
strchr($originalStr, $searchStr, $before_search
Parameter:
- $originalStr: This parameter specifies the string in which the word is to be searched. It is mandatory
- $searchStr: It specifies the word to be searched in the given $originalStr, it can be a character or a number also, if a number is passed, it searches for the equivalent ASCII value character in the $originalStr. It is mandatory.
- $before_search: This is an optional parameter which when set to True returns the part of $originalStr before the first occurrence of $searchStr. It is set to false by default.
- It returns the string starting from the first occurence of the $searchStr in $originalStr to the end of the $originalStr when the $searchStr is found.
- It returns nothing when the $searchStr is not present in the given $originalStr.
- It returns the part of string before the first occurence of the $searchStr when $before_search is set to TRUE.
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- p5.js | hue() function
- p5.js | min() function
- p5.js | red() function
- p5.js | cos() function
- p5.js | sin() function
- p5.js | log() function
- PHP | exp() Function
- p5.js | abs() function
- p5.js | sq() function
- D3.js | d3.map.set() Function
- CSS | var() Function
- D3.js | d3.map.get() Function
- PHP Ds\Set sum() Function
Return Value: It returns a string depending on the below three cases:
Examples:
Input : $originalStr = "geeks for geeks"
$searchStr = "geeks"
Output : geeks for geeks
Input : $originalStr = "geeks for geeks"
$searchStr = "for"
Output : for geeks
Input : $originalStr = "striver has published 180 articles"
$searchStr = "has" $before_search = TRUE
Output : striver
Input: $originalStr = "geeks for geeks" $searchStr = "gfg"
Output: No output
Below programs illustrate the strchr() function in PHP:
Program 1: Program to demonstrate strchr() fucntion when word is found.
<?php // Program to demonstrate the chr() // function when word is found $originalStr = "geeks for geeks"; $searchStr = "geeks" ; // prints the string from the // first occurence of the $searchStr echo strchr($originalStr, $searchStr); ?> |
Output:
geeks for geeks
Program 2: Program to demonstrate strchr() fucntion when word is not found.
<?php // Program to demonstrate the chr() // function when word is not found $originalStr = "geeks for geeks"; $searchStr = "gfg" ; // prints the string from the // first occurence of the $searchStr echo strchr($originalStr, $searchStr); ?> |
Output:
No Output
Program 3: Program to demonstrate strchr() fucntion when word is found and $before_search is set to true.
<?php // Program to demonstrate the chr() // function when word is found and // $before_search is set to true $originalStr = "geeks for geeks"; $searchStr = "for" ; // prints the string from the // first occurence of the word echo strchr($originalStr, $searchStr, true); ?> |
Output:
geeks
Program 4: Program to demonstrate strchr() fucntion when a part of word is passed and found.
<?php // Program to demonstrate the chr() // function when a part of word is passed and found $originalStr = "geeks for geeks"; $searchStr = "eks" ; // prints the string from the // first occurence of the word echo strchr($originalStr, $searchStr); ?> |
Output:
eks for geeks
Program 5: Program to demonstrate strchr() fucntion when a number is passed and its equivalent ASCII character is searched.
<?php // Program to demonstrate the chr() // function when a number is passed and its equivalent // ASCII character is searched $originalStr = "geeks for geeks"; // 101 is the ASCII value of e $searchStr = 101 ; echo strchr($originalStr, $searchStr); ?> |
Output:
eeks for geeks
Reference:
http://php.net/manual/en/function.strchr.php
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



