PHP Strpos () and stripos () Functions

PHP

This function helps us find the position of the first occurrence of a string in another string. This returns an integer value for the position of the first occurrence of the string. This function is case sensitive , which means it treats upper and lower case characters differently.Syntax:
 strpos (original_str, search_str, start_pos)
Parameters used :
Of the three parameters specified in the syntax, two are required and one - optional. The three parameters are described below:
  • original_str (required):this parameter refers to the original string in which we need to search for an occurrence of the required string.
  • search_str (required):this parameter refers to the line we need to find.
  • start_pos (optional): pointsto the line position from which to start search.
  • Return type : This function returns an integer value that represents the index original_str, in which the string search_str first appears.Example:  
    // PHP code to find the position of a specific line
    / / first appearance using case sensitive for the strpos() function function Search ( $search , $string ) { $position = strpos ( $string , $search , 5); if ( $position == true) { return "Found at position:" . $position ; } else { return "Not Found" ; } }  
    // Driver code $string = "Welcome to GeeksforGeeks" ; $search = "Geeks" ; echo Search ( $search , $string ); ?>
    Output:
    Found at position 11
    stripos() in PHP This function also helps us find the position of the first occurrence of a string on another line. This returns an integer value for the position of the first occurrence of the string. This function is case insensitive , which means that it handles both uppercase and lowercase letters the same. This function works the same as strpos(), the difference is that it is case sensitive, whereas strpos() is case sensitive.Syntax:
    stripos (original_str, search_str, start_pos)
    Parameters used :
    Of the three parameters specified in the syntax, two are required and one optional
  • original_str (required):this parameter refers to the original string where we need to search for an occurrence of the required string.
  • search_str (required):this parameter refers to the string we need to find.
  • start_pos (optional):this parameter refers to the position of the string where the search should start. Return type : This function returns an integer value that represents the index original_str at where the string search_str first appears.Example:
    // PHP code to find a specific string
    // first occurrence using stripos() case insensitive function Search ( $search , $string ) { $position = stripos ( $string , $search , 5); if ( $position == true) { return "Found at posiion" . $position ; } else { return "Not Found" ; } }  
    // Driver code $string = "Welcome to GeeksforGeeks" ; $search = "engineer" ; echo Search ( $search , $string ); ?>
    Output:
    Found at position 11