Page 1 of 1

Getting the position of the last occurrence of a string

Posted: Fri Mar 03, 2006 12:19 pm
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

Posted: Fri Mar 03, 2006 12:52 pm
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.

Posted: Fri Mar 03, 2006 1:19 pm
by josh

Posted: Fri Mar 03, 2006 1:27 pm
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!

Posted: Fri Mar 03, 2006 1:50 pm
by evilmonkey
Yeah, I solved it with explode() and array_pop(). Thanks for the idea. :)