[英]How to get last part of a string?
I have this string: 我有这个字符串:
"application/controllers/backend"
I want get: 我想得到:
backend
of course the backend
it's dynamic, so could be change, so I'm looking for a solution that allow me to get only the last part of the string. 当然,
backend
是动态的,因此可以更改,因此我正在寻找一种解决方案,使我仅能获取字符串的最后一部分。 How I can do that? 我该怎么做?
You can take the advantage of basename()
to get the last part 您可以利用
basename()
来获得最后一部分
in your case, it will be 在你的情况下,它将是
basename("application/controllers/backend");
Output: 输出:
backend
$arr = explode ("/", $string);
//$arr[2] is your third element in the string
http://php.net/manual/en/function.explode.php http://php.net/manual/zh/function.explode.php
Some thing like this : 像这样的东西:
echo end(explode("/", $url));
If this thorws error then do : 如果出现此错误,请执行以下操作:
$parts = explode("/", $url);
echo end($parts);
Just use 只需使用
basename("application/controllers/backend");
http://php.net/manual/en/function.basename.php http://php.net/manual/zh/function.basename.php
You did ask initially for a solution with regex, so, although the other answers haven't involved regex, here is one approach which does. 最初您确实要求使用正则表达式解决方案,因此,尽管其他答案没有涉及正则表达式,但这是一种可行的方法。
You can use preg_match
and str_replace
for this: 您可以为此使用
preg_match
和str_replace
:
$string = '"application/controllers/backend"';
preg_match('/[^\/]+"/', $string, $matches);
$last_item = str_replace('"','',$matches[0]);
$last_item
is now a string containing the word backend
. $last_item
现在是一个包含单词backend
的字符串。
而且,如果您想使用正则表达式:
$result = (preg_match('%.*[/\\\\](.*?)$%', $url, $regs)) ? $regs[1] : '';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.