我需要提取URL的某个部分。从查询字符串的URL中提取部分
实施例:
http://www.domain.com/blog/entry-title/?standalone=1
是给定的URL。
blog/entry-title
应该被提取。
但是,提取也应该与http://www.domain.com/index.php/blog/[…]
一起使用作为给定的URL。
此代码适用于内容管理系统。
我已经想出是这样的:
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
),但。
谢谢吨家伙,并有乐趣扭动你的大脑。也许我们可以用正则表达式来做这件事。
我忘了提,在index.php可以在物理目录,例如'http:// www.domain.com/cms /'。我的代码到目前为止工作,并通过你现在引用我的'parse_url'函数也忽略了查询字符串的额外参数。 干杯伙计! – Daniel 2010-06-20 19:36:18