PHP strrpos() Function – Description, Parameters, Return Value, and Examples
The article explains PHP's strrpos() function, detailing its signature, how it finds the last occurrence of a needle in a haystack string, the meaning of its parameters and offset, the return values, and provides multiple code examples illustrating typical usage.
strrpos() calculates the position of the last occurrence of a needle string within a haystack string.
Signature:
int strrpos ( string $haystack , string $needle [, int $offset = 0] )Description: Returns the numeric position of the last occurrence of $needle in $haystack. In PHP 4, $needle must be a single character; if a string is given, only its first character is used.
Parameters: $haystack – the string to search in. $needle – the string to search for; if not a string it is converted to an integer character code. $offset – optional offset; negative values start counting from the end of the string.
Return value: The position of $needle if found, otherwise FALSE.
Examples:
<?php
$foo = "0123456789a123456789b123456789c";
var_dump(strrpos($foo, '7', -5)); // from 5th position from the end, result: int(17)
var_dump(strrpos($foo, '7', 20)); // from position 20, result: int(27)
var_dump(strrpos($foo, '7', 28)); // result: bool(false)
?>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
