PHP String strrpos() functionThe strrpos() is an in-built function of PHP which is used to find the position of the last occurrence of a substring inside another string. It is a case-sensitive function of PHP, which means it treats uppercase and lowercase characters differently. The strrpos() is similar to the strripos(), which is also used to find the last occurrence of the substring inside another string, but strripos() is a case-insensitive function whereas strrpos() is a case-sensitive function. PHP 4+ versions support this function. There are some related functions of PHP which is similar to the strrpos() function: Related functions
SyntaxThe syntax for the following function strrpos() is: ParameterThe strrpos() function accepts three parameters in which two are mandatory, i.e., the main string and the search string. The third parameter is optional, that is $start, from where we start the search of the string. $string (required) - It is a mandatory parameter in which we search the occurrence of the $search string. $search (required) - It is also a mandatory parameter which specifies the string which is to be searched. $start (optional) - It is the last and optional parameter of this function which specifies that from where to begin the search. This parameter has an integer value. Return ValuesThe strrpos() function returns the position of the last occurrence of a substring inside another string. If the string is not found, then it will return FALSE. It is important to note that the string position starts from 0, not from 1. Changelogs
ExamplesThere are some detailed examples to learn the functionality of the strrpos() function. These examples will provide the basic knowledge of this function. Example 1 Below is the basic example of strrpos(): Output: The output for the above program will be- The last occurrence of the search string is found at position: 19 Example 2 Output: In this above example, the last occurrence of "l" is 16. The last occurrence of the search string is found at position: 16 Example 3: Case-sensitive Output: This example has proved that strrpos() is a case-sensitive function, as it treated "COME" and "come" differently. The output for the above program will be- Search string is not found, so it returned: bool(false) Example 4 In this example, the search string is not available in the main string, so it will return Boolean value FALSE. Output: Echo is not sufficient to display the Boolean value, so we use var_dump() function to print that Boolean value FALSE. Search string is not found so it returned: bool(false) Example 5 Below example contains if-else condition. If the string is not found, it will show that search string is not found otherwise, it will display the position of the last occurrence of the search string. Output: Sorry! cml is not found in the string The following search string ome is found at position: 4 Example 6: By using length parameter Output: In the above example, "Wel" is present in the main string; still it displays the output that "search string is not found." It is because the searching is started from 7th position, but "Wel" is available at 1st position. Search string is not found. Example 7 Output: In the above example, strrpos() start searching from 4th position. It found search string at 12th position. ava is found at position 12 Next TopicPHP String Functions |