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:

  1. $originalStr: This parameter specifies the string in which the word is to be searched. It is mandatory
  2. $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.
  3. $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.
  4. Return Value: It returns a string depending on the below three cases:

    • 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.
    • 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.

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <?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);
      ?>

      chevron_right

      
      

      Output:

      geeks for geeks
      

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

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <?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);
      ?>

      chevron_right

      
      

      Output:

      No Output
      

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

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <?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);
      ?>

      chevron_right

      
      

      Output:

      geeks
      

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

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <?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);
      ?>

      chevron_right

      
      

      Output:

      eks for geeks
      

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

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <?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);
      ?>

      chevron_right

      
      

      Output:

      eeks for geeks
      

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



      My Personal Notes arrow_drop_up

      Striver(underscore)79 at Codechef and codeforces D

      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.




      Article Tags :
      Practice Tags :


      Be the First to upvote.


      Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.