00 Votes

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

Question by Compi | 2016-06-06 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?

ReplyPositiveNegative
Avatar
1Best Answer1 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 2017-12-19 | Created on 2016-06-07

ReplyPositive Negative
Reply

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.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.