PHP strrpos() Function: Syntax, Examples
The strrpos() function in PHP is a built-in function which is used to find the position of the last occurrence of a substring within a given string.
If the substring is found, the function returns the index (i.e. numeric position) of the last occurrence of a substring within a string. If not found, it returns false.
Syntax of strrpos() Function in PHP
The general syntax to define strrpos() function in PHP is as follows:
strrpos(string $haystack, string $needle, int $offset = 0): int|false
The strrpos() function has three parameters: $haystack, $needle, and $offset.
- $haystack is the input string where you want to search in.
- $needle specifies the substring or character you are trying to find inside the haystack (i.e. input string).
- $offset is an integer that represents the numeric index (or position) at which the strrpos() function starts the search. The default value of $offset is 0, which means the search starts from the beginning of the string.
If $offset is positive, the strrpos() function will start the search at $offset number of characters counted from the starting of the string. If negative, the search starts at $offset number of characters counted from the end of the string.
The strrpos() function will throw a ValueError if the offset is invalid or exceeds string length. The first two parameters are required, while the third is optional.
Return Value
The strrpos() function returns the position (as an integer) of the last occurrence of the $needle (substring) in the $haystack string. The position returned is zero-based, meaning the first character in the string is at position 0. If the $needle is not found, the function returns false.
Basic Examples of PHP strrpos() Function
Example 1: Let’s write a PHP program to search for the position of the last occurrence of a substring within a main string using the strrpos() function.
<?php
$text = "PHP is a powerful scripting language. Many developers use PHP.";
$position = strrpos($text, "PHP");
echo "The last occurrence of 'PHP' is at position: " . $position;
?>
Output: The last occurrence of 'PHP' is at position: 58
In this example, we have defined a variable named $text and assigned it with a string value “PHP is a powerful scripting language. Many developers use PHP.”. Then, we have called strrpos() function to search the string from the end and find the last occurrence of “PHP”.
In this case, the substring “PHP” appears twice in the string. First is at the position 0 from beginning and the second at position 53 near the end. So, the strrpos() function returns 53. This is a simple example to demonstrate how strrpos() function provided by PHP locates the last position of a substring within a string.
Example 2: PHP program to find the last occurrence of a character in a specified string.
<?php
$text = "PHP is a powerful scripting language.";
$position = strrpos($text, "a");
echo "The last occurrence of character 'a' is at position: " . $position;
?>
Output: The last occurrence of character 'a' is at position: 33
Advanced Examples of strrpos() Function
Example 3: Program to find the last occurrence of a character in a specified string using the strrpos() function with an offset.
<?php
$text = "PHP is a powerful scripting language.";
$position = strrpos($text, "a", -10);
echo "The last occurrence of character 'a' is at position: " . $position;
?>
Output: The last occurrence of character 'a' is at position: 7
In this example, we are looking for the last occurrence of character ‘a’. We have used the strrpos() function with offset which set to -10. This means that the function will start searching from 10th character from the end of the string. So, the function finds the first character “a” at position 7, and returns it. We have defined a variable $position and stored the returned character in it.
Note: This example only works in PHP 8.0 and above version, because earlier versions do not support negative offsets in strrpos() function.
Example 4: PHP program to perform case-sensitive search using strrpos() function.
<?php
$text = "PHP is a powerful scripting language.";
$position = strrpos($text, "php");
var_dump($position);
?>
Output: bool(false)
In this example, we are performing a case-sensitive search using strrpos() function. Since the strrpos() function is a case-sensitive, it will treat the uppercase (‘A’) and lowercase (‘a’) as different characters. If the case of the character or substring (i.e. $needle) does not match exactly in the main string (i.e. $haystack), the function returns false. In this case, use strripos() function if you want to ignore case differences during the search.
Conclusion
In this tutorial, you learned how to use the strrpos() function in PHP to find the last occurrence of a substring within a string. This function is case-sensitive. If you need a case-insensitive search, use the strripos() function.