PHP strripos | How to used PHP strripos and How it works?

Summary: In this article, you will learn how to use PHP strripos to find the position of the last occurrence of a case-insensitive string in a string. Let’s understand the PHP strripos() function with an example.

Definition and Usage PHP strripos() Function

  • The PHP strripos() function is used to find the position of the last occurrence of a case-insensitive substring in a string or in another way we can say that to finds the position of the last occurrence of a string inside another string.
  • The PHP strripos() function is case-sensitive.
.
Note: Version:- PHP 5, PHP 7, PHP 8
Related functions:
  • stripos() – This stripos() function is applied in PHP to finds the position of the first occurrence of a string inside another string. it’s a case-insensitive
  • strpos() – This strpos() function is applied in PHP to finds the position of the first(1st) occurrence of a string inside another string. it’s a case-sensitive
  • strrpos() – This strrpos() function is applied in PHP to finds the position of the last occurrence of a string inside another string. it’s a case-sensitive

Syntax

strripos ( string $haystack , string $needle , int $offset = 0 ) : int|false

Find the numeric position of the last occurrence of a needle in the haystack string.
Unlike the strrpos(), strripos() is case-insensitive.

Parameter Values
Parameter Value Description
haystack (Required) The string to search in.
needle (Required) Prior to PHP 8.0.0, if the needle isn’t a string, furthermore, it is converted to an integer and applied as the ordinal value of a character. This behavior is deprecated as of PHP 7.3.0, & relying on it is highly discouraged. it is Depending on the intended behavior, the needle should either be explicitly cast to a string, or an explicit call to chr should be performed.
offset (Optional) If positive or zero, the search is performed left to right skipping the first(1st) offset bytes of the haystack.
If negative, the search is performed right to left skipping the last offset bytes of the haystack & searching for the first occurrence of needle.
.
Important Note: it is effectively looking for the last occurrence of needle before the last offset bytes.

Return Values

Returns the position of where the needle exists relative to the beginning or start of the haystack string (independent of search direction or offset). Important note that string positions start or begin at 0, and not 1.
Returns false if the needle wasn’t found.

.
Warning: This function may return Boolean false, but, may it also return a non-Boolean value that evaluates to false. Furthermore, used the === operator for testing the return value of this function.

Example of PHP strripos() Function

Example 1:
Output: Hey Congratulations! We found the last (xY) in (xyxyz) at position (2)
Example 2:
The output of the above code segment: 11

Conclusion

In this article, you have learned how to use PHP strripos Function to find the position of the last occurrence of a case-insensitive string in a string. I hope you will enjoy it!. If have any problem, contact me on info@tutorialscan.com.