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.
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
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. |
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.
Example of PHP strripos() Function
Example 1:
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $haystack = 'xyxyz'; $needle = 'xY'; $position = strripos($haystack, $needle); if ($position === false) { echo "Sorry, we did not find ($needle) in ($haystack)"; } else { echo "Hey Congratulations!\n"; echo "We found the last ($needle) in ($haystack) at position ($position)"; } ?> |
Example 2:
|
1 2 3 |
<?php echo strripos("Welcome to Tutorialscan website!","Tutorialscan"); ?> |
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.