How to php regular expression get the final part of the url?
HI, if there have many url address.
http://www.aaa.com/dd/cc/ee
http://www.bbb.com/ff/gg
http://www.ccc.com/hh/jj/kk/ll
how to use php r开发者_如何转开发egular expression to get the final part of the url?
ee
,
gg
,
ll
It might be easier to use basename()
instead of a regex:
basename('http://www.ccc.com/hh/jj/kk/ll', '/');
Why do you want to use regular expressions? Why not use parse_url
function getFinalPathPartOfUrl($url) {
$path = parse_url($url, PHP_URL_PATH);
$paths = explode('/', $path);
return end($paths);
}
/(.+?)$
That should do...
You can use:
$matches = array();
preg_match ('/\/([a-z])+$/', $url, $matches);
var_dump($matches[1]);
This gets any alphabetic characters trailing the final forward slash, anchored ad the end of the string. If you want to include digits or other characters, add them between the []
.
If you don't need Regex and there aren't any files or queries, use:
$dir = array_pop(explode("/", "http://www.ccc.com/hh/jj/kk/ll"));
精彩评论