我需要提取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