0 0 Votes

PHP: strpos from behind - position of last occurrence in string

Question by Compi | 06/06/2016 at 18:50

I am searching for a PHP function returning the position of the last occurrence of a string in another string.

So, I need something like the function strpos, but it should search beginning from the end of the string instead of beginning at the first character of the string. As an example, when searching for "b" within the string "abcba", I will get the position of the last "b" and not of the first one.

Is there such a function available in PHP? Or do I have to write an own function for that purpose?


Anja Proggy

Show Profile | Message
1Best Answer
1 Vote

In addition to the function strpos(), which counts from the front, there is the function strrpos(), which counts from the back (string-right-position).

In this small example, you can see the difference between both functions:

echo strpos("abcba", "b");   //  1  
echo strrpos("abcba", "b");  //  3

The function strrpos() searches case-sensitive (the uppercase and lowercase writing of the words makes a difference). If the uppercase and lowercase writing should not make a difference you can use the case insensitive function strripos() instead.
Last Update on 19/12/2017 | Created on 07/06/2016

ReplyPositive Negative

Related Topics

PHP: Line break and strpos
Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own articles on askingbox.com. How to do.