I need a specific part of the extracted URL.
Example:
http://www.domain.com/blog/entry-title/?standalone=1 - this URL.
blog/entry-title should be retrieved.
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.
I already came up with the following:
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 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; }
However, this code does not care about the arguments at the end of the URL (for example ?standalone=1 ). However, it works for anchors ( #read-more ).
Thanks to the guys from the ton and have fun twisting your brains. Maybe we can do this shit with a regular expression.
source share