从URL中提取查询字符串的一部分
我需要提取URL的特定部分.
I need a certain part of a URL extracted.
示例:
http://www.domain.com/blog/entry-title/?standalone=1
是给定的URL.
blog/entry-title
应该被提取.
但是,提取也应该使用http://www.domain.com/index.php/blog/[…]
作为给定的URL.
However, the extraction should also work with http://www.domain.com/index.php/blog/[…]
as the given URL.
此代码用于内容管理系统.
This code is for a Content Management System.
我已经想到的是这样:
function getPathUrl() {
$folder = explode('/', $_SERVER['SCRIPT_NAME']);
$script_filename = pathinfo($_SERVER['SCRIPT_NAME']); // supposed to be 'index.php'
$request = explode('/', $_SERVER['REQUEST_URI']);
// first element is always ""
array_shift($folder);
array_shift($request);
// now it's only the request url. filtered out containing folders and 'index.php'.
$final_request = array_diff($request, array_intersect($folder, $request));
// the indexes are mangled up in a strange way. turn 'em back
$final_request = array_values($final_request);
// remove empty elements in array (caused by superfluent slashes, for instance)
array_clean($final_request);
// make a string out of the array
$final_request = implode('/', $final_request);
if ($_SERVER['QUERY_STRING'] || substr($final_request, -1) == '?') {
$final_request = substr($final_request, 0, - strlen($_SERVER['QUERY_STRING']) - 1);
}
return $final_request;
}
但是,此代码不处理URL末尾的参数(如?standalone=1
).不过,它适用于锚点(#read-more
).
However, this code does not take care of the arguments at the end of the URL (like ?standalone=1
). It works for anchors (#read-more
), though.
感谢很多人,并且开心地绞尽脑汁.也许我们可以用正则表达式来做到这一点.
Thanks a ton guys and have fun twisting your brains. Maybe we can do this shit with a regular expression.
有许多示例和您想要的信息:
There's many examples and info for what you want at: