PHP String strripos() Function
The PHP String strripos() function is a predefined function which is used to find the place of the last occurrence of a string within another string. This function accepts three parameters - string, search and offset. It is case-insensitive unlike the strrpos().
As of PHP 5.0, the search parameter can now be a string of more than one character. The offset argument was added to PHP 5.0
Syntax
Below is the syntax of the PHP String strripos() function −
int strripos ( string $string, string $search, int $offset = 0 )
Parameters
Here are the parameters of the strripos() function −
$string − (Required) It is used to specify the string to search.
$search − (Required) It is the string to search.
$offset − (Optional) It is the specific place where to start the search.
Return Value
The strripos() function returns the position of the last occurrence of a string. Otherwise it returns FALSE. String position starts at 0 and not 1.
Warning
This function may return the Boolean false, but it can also return a non-Boolean value that evaluates to false.
PHP Version
First introduced in core PHP 5, the strripos() function continues to function easily in PHP 7, and PHP 8.
Example 1
First we will show you the basic example of the PHP String strripos() function to find the position of the last occurrence of "Tutorialspoint" inside the given string.
<?php
echo strripos("I love Tutorialspoint, I love Tutorialspoint too!","Tutorialspoint");
?>
Output
Here is the outcome of the following code −
30
Example 2
In this example we will use the strripos() function and find the last occurrence of "world" which is case-insensitive.
<?php // Define string here $string = "Hello World, welcome to the programming world!"; $search = "world"; // Find the last occurrence of the substring $position = strripos($string, $search); echo "The last occurrence of '$search' is at position: $position."; ?>
Output
This will generate the below output −
The last occurrence of 'world' is at position: 40.
Example 3
Now the below code uses strripos() function to handle the case when substring is not found.
<?php
// Define string here
$string = "PHP is a popular scripting language!";
$search = "Python";
// Attempt to find the last occurrence
$position = strripos($string, $search);
if ($position === false) {
echo "'$search' was not found in the string.";
} else {
echo "The last occurrence of '$search' is at position: $position.";
}
?>
Output
This will create the below output −
'Python' was not found in the string.