strrpos

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

Description

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

Find the numeric position of the last occurrence of
needle in the haystack string.

Parameters

haystack

The string to search in.

needle

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

offset

If specified, search will start this number of characters counted from the
beginning of the string. If the value is negative, search will instead start
from that many characters from the end of the string, searching backwards.

Return Values

Returns the position where the needle exists relative to the beginning of
the haystack string (independent of search direction
or offset).
Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

Warning

This function may
return Boolean FALSE, but may also return a non-Boolean value which
evaluates to FALSE. Please read the section on Booleans for more
information. Use the ===
operator for testing the return value of this
function.

Changelog

Version

Description

5.0.0

The needle may now be a string of more than one
character.

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:

User Contributed Notes 32 notes

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 offset is positive, then strrpos only operates on the part of the string from offset to the end. This will usually have the same results as not specifying an offset, unless the only occurences of needle are before offset (in which case specifying the offset won't find the needle).

- If offset is negative, then strrpos only operates on that many characters at the end of the string. If the needle is farther away from the end of the string, it won't be found.

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.

There are a lot of alternative - and unfortunately buggy - implementations of strrpos() (or last_index_of as it was called) on this page. This one is a slight modifiaction of the one below, but it should world like a *real* strrpos(), because it returns false if there is no needle in the haystack.

<?php
###################################################
#
# DESCRIPTION:
# This function returns the last occurance of a string,
# rather than the last occurance of a single character like
# strrpos does. It also supports an offset from where to
# start the searching in the haystack string.
#
# ARGS:
# $haystack (required) -- the string to search upon
# $needle (required) -- the string you are looking for
# $offset (optional) -- the offset to start from
#
# RETURN VALS:
# returns integer on success
# returns false on failure to find the string at all
#
###################################################

I should have looked here first, but instead I wrote my own version of strrpos that supports searching for entire strings, rather than individual characters. This is a recursive function. I have not tested to see if it is more or less efficient than the others on the page. I hope this helps someone!

refering to the comment and function about lastIndexOf()...
It seemed not to work for me the only reason I could find was the haystack was reversed and the string wasnt therefore it returnt the length of the haystack rather than the position of the last needle... i rewrote it as fallows:

this one fallows the strpos syntax rather than java's lastIndexOf.
I'm not positive if it takes more resources assigning all of those variables in there but you can put it all in return if you want, i dont care if i crash my server ;).

If my math is off, please feel free to correct. - A positive offset will be the minimum character index position of the first character allowed. - A negative offset will be subtracted from the total length and the position directly before will be the maximum index of the first character being searched.

returns the character index ( 0+ ) of the last occurence of the needle.

* boolean FALSE will return no matches within the haystack, or outside boundries specified by the offset.

To begin, i'm sorry for my English.So, I needed of one function which gives me the front last position of a character. Then I said myself that it should be better to make one which gives the "N" last position.

What the heck, I thought I'd throw another function in the mix. It's not pretty but the following function counts backwards from your starting point and tells you the last occurrance of a mixed char string:

I've got a simple method of performing a reverse strpos which may be of use. This version I have treats the offset very simply:Positive offsets search backwards from the supplied string index.Negative offsets search backwards from the position of the character that many characters from the end of the string.

Here is an example of backwards stepping through instances of a string with this function:

If in the needle there is more than one character then in php 4 we can use the above statement for finding the position of last occurrence of a substring in a string instead of strrpos. Because in php 4 strrpos uses the first character of the substring.

I was having some issues when I moved my code to run it on a different server.The earlier php version didn't support more than one character needles, so tada, bugs. It's in the docs, i'm just pointing it out in case you're scratching your head for a while.