PHP | strrpos() and strripos() Functions
We already have learned about strpos() and stripos() which helps to find the first occurrence of a string in another string. In this article we will learn about two other similar functions strrpos() and strripos().
strrpos() in PHP
Unlike strpos(), strrpos() function helps us to find the position of the last occurrence of a string in another string. This function returns an integer value corresponding to the position of the last occurrence of the string. This function is case-sensitive, which means it treats uppercase and lowercase characters differently.
Syntax:
strrpos(original_str, search_str, start_pos)
Parameters
Out of the three parameters specified in the syntax, two are mandatory and one is optional. The three parameters are described below:
- original_str (mandatory):This parameter refers to the original string in which we need to search the occurrence of required string.
- search_str (mandatory): This parameter refers to the string that we need to search.
- start_pos (optional): Refers to the position of the string from where the search must begin.
Return Type: This function returns an integer value which represents the index of original_str where the string search_str last occurs.
Example:
<?php // PHP code to search for a specific string's position // last occurrence using strpos() case-sensitive function function Search( $search , $string ){ $position = strrpos ( $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 19
strripos() in PHP
Unlike stripos(), strripos() function helps us to find the position of the last occurrence of a string in another string. This function returns an integer value corresponding to the position of the last occurrence of the string. This function is case-insensitive, which means it treats uppercase and lowercase characters equally.
Syntax:
strrpos(original_str, search_str, start_pos)
Parameters
Out of the three parameters specified in the syntax, two are mandatory and one is optional. The three parameters are described below:
- original_str (mandatory):This parameter refers to the original string in which we need to search the occurrence of required string.
- search_str (mandatory): This parameter refers to the string that we need to search.
- start_pos (optional): Refers to the position of the string from where the search must begin.
Return Type: This function returns an integer value which represents the index of original_str where the string search_str last occurs.
Example:
<?php // PHP code to search for a specific string // last occurrence using stripos() case-insensitive function function Search( $search , $string ){ $position = strripos ( $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 19
Recommended Posts:
- Difference between regular functions and arrow functions
- PHP | Functions
- ES6 | Functions
- PHP | die() & sleep() functions
- PHP | log(), log10() Functions
- PHP | String Functions
- Array of functions in JavaScript
- jQuery | Callback Functions
- PHP | Ds\Map Functions Complete Reference
- PHP | Ds\Set Functions Complete Reference
- PHP | GMP Functions Complete Reference
- PHP | startsWith() and endsWith() Functions
- CSS | Functions Complete Reference
- How to add many functions in one ng-click directive?
- PHP | strpos() and stripos() Functions
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.