Getting the position of the last occurrence of a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Getting the position of the last occurrence of a string

Post by evilmonkey »

Is there a way to do that in PHP? For example, path/to/folder/with/an/image.jpg. I only want the image.jpg part. What's the easiest way to do this?

Thanks! :D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think you can use explode() to split it on the '/' or you can reverse the string and then strpos everything after the '/' then reverse the string back.

Just a thought. I am sure there is an easier way to do it, but I have done this before to get what you are after.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Oh crap, forgot to put that one in there. I used to use a combination of substr and strpos to get the name portion of the current script before I read here that you can use basename().

I love these forums!
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Yeah, I solved it with explode() and array_pop(). Thanks for the idea. :)
Post Reply