Skip to content
Related Articles

Related Articles

PHP | strchr() Function
  • Last Updated : 02 Nov, 2020

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.

Return Value: It returns a string depending on the below three cases: 



  • It returns the string starting from the first occurrence 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 occurrence of the $searchStr when $before_search is set to TRUE.

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() function when word is found. 

PHP




<?php
// Program to demonstrate the strchr()
// function when word is found
$originalStr = "geeks for geeks";
$searchStr = "geeks" ;
 
// prints the string from the
// first occurrence of the $searchStr
echo strchr($originalStr, $searchStr);
?>

Output: 

geeks for geeks


Program 2: Program to demonstrate strchr() function when word is not found. 

PHP




<?php
// Program to demonstrate the strchr()
// function when word is not found
$originalStr = "geeks for geeks";
$searchStr = "gfg" ;
 
// prints the string from the
// first occurrence of the $searchStr
echo strchr($originalStr, $searchStr);
?>

Output: 

No Output



Program 3: Program to demonstrate strchr() function when word is found and $before_search is set to true. 

PHP




<?php
// Program to demonstrate the strchr()
// function when word is found and
// $before_search is set to true
$originalStr = "geeks for geeks";
$searchStr = "for" ;
 
// prints the string from the
// first occurrence of the word
echo strchr($originalStr, $searchStr, true);
?>

Output: 

geeks


Program 4: Program to demonstrate strchr() function when a part of word is passed and found. 

PHP




<?php
// Program to demonstrate the strchr()
// function when a part of word is passed and found
$originalStr = "geeks for geeks";
$searchStr = "eks" ;
 
// prints the string from the
// first occurrence of the word
echo strchr($originalStr, $searchStr);
?>

Output: 

eks for geeks


Program 5: Program to demonstrate strchr() function when a number is passed and its equivalent ASCII character is searched. 

PHP




<?php
// Program to demonstrate the strchr()
// 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
 

My Personal Notes arrow_drop_up
Recommended Articles
Page :