PYTHON strrpos

is this article helpful?
Python replacement for PHP's strrpos [ edit | history ]
# returns -1 when not found:
pos = haystack.rfind(needle)
pos = haystack.rfind(needle, offset)

# raises ValueError when not found:
pos = haystack.rindex(needle)
pos = haystack.rindex(needle, offset)

PHP strrpos

PHP original manual for strrpos [ show | ]


(PHP 4, PHP 5)

strrposFind position of last occurrence of a char in a string


int strrpos ( string $haystack , string $needle [, int $offset= 0 ] )

Returns the numeric position of the last occurrence of needle in the haystack string. Note that the needle in this case can only be a single character in PHP 4. If a string is passed as the needle, then only the first character of that string will be used.

If needle is not found, returns FALSE.

It is easy to mistake the return values for "character found at position 0" and "character not found". Here's how to detect the difference:


// in PHP 4.0.0 and newer:
$pos strrpos($mystring"b");
if (
$pos === false) { // note: three equal signs
    // not found...

// in versions older than 4.0.0:
$pos strrpos($mystring"b");
if (
is_bool($pos) && !$pos) {
// not found...

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

Note: As of PHP 5.0.0 offset may be specified to begin searching an arbitrary number of characters into the string. Negative values will stop searching at an arbitrary point prior to the end of the string.

Note: The needle may be a string of more than one character as of PHP 5.0.0.





Return Values

See Also

  • strpos() - Find position of first occurrence of a string
  • strripos() - Find position of last occurrence of a case-insensitive string in a string
  • strrchr() - Find the last occurrence of a character in a string
  • substr() - Return part of a string
  • stristr() - Case-insensitive strstr
  • strstr() - Find first occurrence of a string