Tutorialspoint.dev

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.

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



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter