PHP: Hypertext Preprocessor (original) (raw)

strrpos

(PHP 4, PHP 5, PHP 7, PHP 8)

strrpos — Find the position of the last occurrence of a substring in a string

Description

Parameters

haystack

The string to search in.

needle

The string to search for.

Prior to PHP 8.0.0, if needle is not a string, 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, and relying on it is highly discouraged. Depending on the intended behavior, theneedle should either be explicitly cast to string, or an explicit call to chr() should be performed.

offset

If zero or positive, the search is performed left to right skipping the first offset bytes of thehaystack.

If negative, the search starts offset bytes from the right instead of from the beginning of haystack. The search is performed right to left, searching for the first occurrence of needle from the selected byte.

Note:

This is effectively looking for the last occurrence ofneedle at or before the lastoffset bytes.

Return Values

Returns the position where the needle exists relative to the beginning of the haystack string (independent of search direction or offset).

Note: String positions start at 0, and not 1.

Returns [false](reserved.constants.php#constant.false) if the needle was not found.

Warning

This function may return Boolean [false](reserved.constants.php#constant.false), but may also return a non-Boolean value which evaluates to [false](reserved.constants.php#constant.false). Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Changelog

Version Description
8.0.0 needle now accepts an empty string.
8.0.0 Passing an int as needle is no longer supported.
7.3.0 Passing an int as needle has been deprecated.

Examples

Example #1 Checking if a needle is in the haystack

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:

`<?php

$pos

= strrpos($mystring, "b");
if ($pos === false) { // note: three equal signs
// not found...
}?>`

Example #2 Searching with offsets

<?php $foo = "0123456789a123456789b123456789c";// Looking for '0' from the 0th byte (from the beginning) var_dump(strrpos($foo, '0', 0));// Looking for '0' from the 1st byte (after byte "0") var_dump(strrpos($foo, '0', 1));// Looking for '7' from the 21th byte (after byte 20) var_dump(strrpos($foo, '7', 20));// Looking for '7' from the 29th byte (after byte 28) var_dump(strrpos($foo, '7', 28));// Looking for '7' right to left from the 5th byte from the end var_dump(strrpos($foo, '7', -5));// Looking for 'c' right to left from the 2nd byte from the end var_dump(strrpos($foo, 'c', -2));// Looking for '9c' right to left from the 2nd byte from the end var_dump(strrpos($foo, '9c', -2)); ?>

The above example will output:

int(0) bool(false) int(27) bool(false) int(17) bool(false) int(29)

See Also

Found A Problem?

brian at enchanter dot net

17 years ago

`The documentation for 'offset' is misleading.

It says, "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."

This is confusing if you think of strrpos as starting at the end of the string and working backwards.

A better way to think of offset is:

If, for example, you want to find the last space in a string before the 50th character, you'll need to do something like this:

strrpos($text, " ", -(strlen($text) - 50));

If instead you used strrpos($text, " ", 50), then you would find the last space between the 50th character and the end of the string, which may not have been what you were intending.

`

dave at pixelmetrics dot com

5 years ago

`The description of offset is wrong. Here’s how it works, with supporting examples.

Offset effects both the starting point and stopping point of the search. The direction is always right to left. (The description wrongly says PHP searches left to right when offset is positive.)

Here’s how it works:
When offset is positive, PHP searches right to left from the end of haystack to offset. This ignores the left side of haystack.

When offset is negative, PHP searches right to left, starting offset bytes from the end, to the start of haystack. This ignores the right side of haystack.

Example 1:
$foo = ‘aaaaaaaaaa’;
var_dump(strrpos($foo, 'a', 5));
Result: int(10)

Example 2:
$foo = "aaaaaa67890";
var_dump(strrpos($foo, 'a', 5));
Result: int(5)

Conclusion: When offset is positive, PHP searches right to left from the end of haystack.

Example 3:
$foo = "aaaaa567890";
var_dump(strrpos($foo, 'a', 5));
Result: bool(false)

Conclusion: When offset is positive, PHP stops searching at offset.

Example 4:
$foo = ‘aaaaaaaaaa’;
var_dump(strrpos($foo, 'a', -5));
Result: int(6)

Conclusion: When offset is negative, PHP searches right to left, starting offset bytes from the end.

Example 5:
$foo = "a234567890";
var_dump(strrpos($foo, 'a', -5));
Result: int(0)

Conclusion: When offset is negative, PHP searches right to left, all the way to the start of haystack.

`

Daniel Brinca

17 years ago

`Here is a simple function to find the position of the next occurrence of needle in haystack, but searching backwards (lastIndexOf type function):

//search backwards for needle in haystack, and return its position
function rstrpos ($haystack, needle,needle, needle,offset){
size=strlen(size = strlen (size=strlen(haystack);
pos=strpos(strrev(pos = strpos (strrev(pos=strpos(strrev(haystack), needle,needle, needle,size - $offset);

if ($pos === false)
return false;

return size−size - sizepos;
}

Note: supports full strings as needle

`

david dot mann at djmann dot co dot uk

7 years ago

`Ten years on, Brian's note is still a good overview of how offsets work, but a shorter and simpler summary is:

strrpos($x, y,50);//1:thistellsstrrpos()whentoSTOP,countingfromtheSTARTofy, 50); // 1: this tells strrpos() when to STOP, counting from the START of y,50);//1:thistellsstrrpos()whentoSTOP,countingfromtheSTARTofx
strrpos($x, y,−50);//2:thistellsstrrpos()whentoSTART,countingfromtheENDofy, -50); // 2: this tells strrpos() when to START, counting from the END of y,50);//2:thistellsstrrpos()whentoSTART,countingfromtheENDofx

Or to put it another way, a positive number lets you search the rightmost section of the string, while a negative number lets you search the leftmost section of the string.

Both these variations are useful, but picking the wrong one can cause some highly confusing results!

`