php 抓取php字符串中最后一个“/”之后的剩余文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2762778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grab remaining text after last "/" in a php string
提问by Roeland
So, lets say I have a $somestringthats holds the value "main/physician/physician_view".
所以,假设我有一个$somestring包含“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".
我只想抓住“physician_view”。如果传递的字符串是“main/physician_view”或“site/main/physician/physician_view”,我希望它也能工作。
Hopefully my question makes sense. Any help would be appreciated!
希望我的问题是有道理的。任何帮助,将不胜感激!
采纳答案by Stephen
There are many ways to do this. I would probably use:
有很多方法可以做到这一点。我可能会使用:
array_pop(explode('/', $string));
回答by Michael Mrozek
回答by smo0f
$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
回答by billynoah
For another one liner, you can use the explode trick and reverse the array:
对于另一个班轮,您可以使用爆炸技巧并反转数组:
current(array_reverse(explode('/',$url)));
回答by BadHorsie
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'
回答by ErickBest
May be Late...
可能会迟到...
The Easiest way inbuilt in PHP to grab the last string after last /could be by simply using the pathinfofunction.
PHP 内置的最简单的方法/是简单地使用该pathinfo函数来获取最后一个字符串。
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
一些小东西,但可以让你成长_灰色_头发_过早
回答by s4suryapal
Simply you can use :
只需您可以使用:
$id = substr( $url, strrpos( $url, '/' )+1 );

