PHP strrpos| Finds the position of the last occurrence of a string?

Summary: In this article, you will learn how to use PHP strrpos to finds the position of the last occurrence of a string inside another string? Let’s understand the PHP strrpos() function with an example.

Definition and Usage PHP strrpos() Function

The PHP strrpos() function used to finds the position of the last occurrence of a string inside another string.
The strrpos() function used to find the numeric position of the last presence of a character in a string. this is a PHP in-build function. this function is case-sensitive, which means this PHP strrpos() function treats uppercase and lowercase characters differently.

The PHP strrpos() function is similar to the PHP strripos() function, which is also used to find the last occurrence of the substring inside another string, but the PHP strripos() function is a case-insensitive function whereas PHP strrpos() is a case-sensitive function.

.
Note: The PHP strrpos() function is a in-build function.
It is a case-sensitive

There are few related PHP functions that are similar to the strrpos() function:

  • stripos() – used to finds the position of the first(1st) occurrence of a string inside another string. It is a Case-insensitive.
  • strpos() – Finds the position of the first occurrence of a string inside another string. It is a Case-sensitive.
  • strripos() – Finds the position of the last occurrence of a string inside another string. It is a Case-insensitive.

Syntax

The following PHP strrpos() function syntax is:

strrpos ($string, $search, $start)
Parameter

The strrpos() function in PHP accepts three parameters during which two are mandatory, i.e., the main string and the search string. The 3rd parameter is optional, that is $start, from where we start the search of the string.

Value Description
$string Required. It is a mandatory/compulsory parameter in which we search the occurrence of the $search string.
$search Required. It is also a mandatory parameter that specifies the string which is to be searched.
$start Optional. It is the last and optional parameter of this function specifies that where to begin the search. This parameter has an integer value.
Return Values

The PHP strrpos() in-build function returns the position of the last occurrence of a substring inside another string. If the string isn’t found, then it’ll return FALSE.

.
Important Note: The string position starts from 0, not from 1.
Changelogs
  • PHP 5.0 included a new parameter $start in strrpos() function which defines that from where to begin the search.
  • After PHP 5.0, we can also pass a string in the $search parameter rather than passing only a single character.

PHP strrpos() Examples

Let’s see the following example to learn the functionality which provides the basic knowledge of this function.

Example 1
Output: The last occurrence of the search string is found at position: 28
Example 2
In this above example, the last occurrence of “v” is 18.

Output: The last occurrence of the search string is found at position: 18
Example 3: Case-sensitive
This example has proved that strrpos() function is case-sensitive, as it treated “THE” and “the” differently. The output of the following program is-

Output: Search string is not found, so it returned: bool(false)
Example 4

In the following example, the search string is not available in the main string, so it will return the Boolean value FALSE.

Here, Echo is not more sufficient to display the Boolean value, so here, we use the var_dump() function to print that Boolean value FALSE.

Output: Search string is not found so it returned: bool(false)
Example 5

The following example contains the if-else condition. If the string isn’t found, it’ll show that the search string isn’t found otherwise, it’ll display the position of the last occurrence of the search string.

Output:
Sorry! mal is not found in the string
The following search string sal is found at position: 21

Example 6: By using the length parameter

In the above example, “universal” is present in the main string; furthermore, still it displays the output that “search string is not found.” because the searching is started from the 17th position, but “universal” is available at the 15th position.

Output: Search string is not found.

Example 7

In the above example, strrpos() function start searching from the 11th position. It found the search string in the 15th position.

Output: Found at position 15

Conclusion

In this article, you have learned how to use PHP strrpos to finds the position of the last occurrence of a string inside another string. I hope you will enjoy it!. If have any problem, contact me on info@tutorialscan.com.