HomeLanguagesPHP strchr() Function

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.

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 = "neveropen for neveropen" 
        $searchStr = "neveropen" 
Output : neveropen for neveropen 

Input : $originalStr = "neveropen for neveropen" 
        $searchStr = "for" 
Output : for neveropen 

Input : $originalStr = "striver has published 180 articles"
        $searchStr = "has"    $before_search = TRUE
Output :  striver
 
Input: $originalStr = "neveropen for neveropen" $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 = "neveropen for neveropen"; 
$searchStr = "neveropen" ;
  
// prints the string from the 
// first occurrence of the $searchStr
echo strchr($originalStr, $searchStr);
?>


Output: 

neveropen for neveropen


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 = "neveropen for neveropen"; 
$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 = "neveropen for neveropen"; 
$searchStr = "for" ;
  
// prints the string from the 
// first occurrence of the word
echo strchr($originalStr, $searchStr, true);
?>


Output: 

neveropen


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 = "neveropen for neveropen"; 
$searchStr = "eks" ;
  
// prints the string from the
// first occurrence of the word
echo strchr($originalStr, $searchStr);
?>


Output: 

eks for neveropen


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 = "neveropen for neveropen"; 
  
// 101 is the ASCII value of e 
$searchStr = 101 ;
  
echo strchr($originalStr, $searchStr);
?>


Output: 

eeks for neveropen


Reference: 
http://php.net/manual/en/function.strchr.php
 

RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS