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

PhpString

Php Problem Overview


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!

Php Solutions


Solution 1 - Php

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

substr($somestring, strrpos($somestring, '/') + 1)

Solution 2 - Php

There are many ways to do this. I would probably use:

array_pop(explode('/', $string));

Solution 3 - Php

Use basename, which was created for this exact purpose.

Solution 4 - Php

$last_part = substr(strrchr($somestring, "/"), 1);

Examples:

php > $a = "main/physician/physician_view";
php > $b = "main/physician_view";
php > $c = "site/main/physician/physician_view";
php > echo substr(strrchr($a, "/"), 1);
    physician_view
php > echo substr(strrchr($b, "/"), 1);
    physician_view
php > echo substr(strrchr($c, "/"), 1);
    physician_view

Solution 5 - Php

The other solutions don't always work, or are inefficient. Here is a more useful general utility function that always works, and can be used with other search terms.

/**
 * Get a substring starting from the last occurrence of a character/string
 *
 * @param  string $str The subject string
 * @param  string $last Search the subject for this string, and start the substring after the last occurrence of it.
 * @return string A substring from the last occurrence of $startAfter, to the end of the subject string.  If $startAfter is not present in the subject, the subject is returned whole.
 */
function substrAfter($str, $last) {
	$startPos = strrpos($str, $last);
	if ($startPos !== false) {
		$startPos++;
		return ($startPos < strlen($str)) ? substr($str, $startPos) : '';
	}
	return $str;
}


// Examples

substrAfter('main/physician/physician_view', '/');  // 'physician_view'

substrAfter('main/physician/physician_view/', '/'); // '' (empty string)

substrAfter('main_physician_physician_view', '/');  // 'main_physician_physician_view'

Solution 6 - Php

May be Late...

The Easiest way inbuilt in PHP to grab the last string after last / could be by simply using the pathinfo function.

In fact, you could check this for yourself,

$urlString = 'path/to/my/xximage.png';

$info = pathinfo($urlString);

You could do:

var_dump($info);

this will give you something like:

'dirname' => string 'path/to/my/' 
'basename' => string 'xximage.png'
'extension' => string 'png' (length=3)
'filename' => string 'xximage' 

so to extract the images out of that link you could do:

 $img=$info['basename'];
 $extension=$info['extension'];
 ///etc...

echo $img.".".$extension; //xximage.png

Something small, but can make you Grow_Gray_Hair_Prematurely

Solution 7 - Php

For another one liner, you can use the explode trick and reverse the array:

current(array_reverse(explode('/',$url)));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoelandView Question on Stackoverflow
Solution 1 - PhpMichael MrozekView Answer on Stackoverflow
Solution 2 - PhpStephenView Answer on Stackoverflow
Solution 3 - PhpoutisView Answer on Stackoverflow
Solution 4 - Phpsmo0fView Answer on Stackoverflow
Solution 5 - PhpBadHorsieView Answer on Stackoverflow
Solution 6 - PhpErickBestView Answer on Stackoverflow
Solution 7 - PhpEaten by a GrueView Answer on Stackoverflow