Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grab remaining text after last "/" in a php string

Tags:

string

php

So, lets say I have a $somestring thats holds the value "main/physician/physician_view".

I want to grab just "physician_view". I want it to also work if the passed string was "main/physician_view" or "site/main/physician/physician_view".

Hopefully my question makes sense. Any help would be appreciated!

like image 559
Roeland Avatar asked May 04 '10 04:05

Roeland


People also ask

How can I get the last part of a string in PHP?

The strlen() is a built-in function in PHP which returns the length of a given string. It takes a string as a parameter and returns its length. It calculates the length of the string including all the whitespaces and special characters.

How do I get everything after a specific character in PHP?

or: $text = end((explode('_', '233718_This_is_a_string', 2))); By specifying 2 for the limit parameter in explode() , it returns array with 2 maximum elements separated by the string delimiter. Returning 2nd element ( [1] ), will give the rest of string.

How do you extract a string from a string in PHP?

Use the PHP substr() function to extract a substring from a string. Use the negative offset to extract a substring from the end of the string. The last character in the input string has an index of -1 . Use the negative length to omit a length number of characters in the returned substring.

How can I get the last word in PHP?

strrpos() Function: This inbuilt function in PHP is used to find the last position of string in original or in another string. It returns the integer value corresponding to position of last occurrence of the string, also it treats uppercase and lowercase characters uniquely.


1 Answers

You can use strrpos() to find the last occurence of one string in another:

substr($somestring, strrpos($somestring, '/') + 1) 
like image 83
Michael Mrozek Avatar answered Oct 01 '22 03:10

Michael Mrozek