Example 1: domain.com/dir_1/dir_2/dir_3/./../../../
Should resolve naturally in the browser into = domain.com/
Example 2: domain.com/dir_1/dir_2/dir_3/./../../../test/././../new_dir/
Should resolve into domain.com/new_dir/
Example 3: domain.com/dir_1/dir_2/dir_3/./../../../test/dir4/../final
Should resolve into domain.com/test/final
The question is... how can i iterate through the string to do this? I feel like the for() loop would get confused at this point..
Transfrom relative path into absolute URL using PHP
and
PHP: How to resolve a relative url
doesn't work for me in this case.. I shouldn't need a reference point (base), since the objective to clean up what I already have..
What you want here is a "replaceDots" function.
It works by remembering the position of the last valid item and then if you get dots then removing the item. The full description is here "Remove Dot Segments" http://tools.ietf.org/html/rfc3986. Search for Remove Dot Segments at the RFC page.
You need more than one loop. The inner loop scans ahead and looks at the next part and then if it is dots the current part is skipped etc, but it can be trickier than that. Or consider breaking it up into parts and then following the algorithm.
While the input buffer is not empty, loop as follows:
A. If the input buffer begins with a prefix of "../" or "./", then remove that prefix from the input buffer; otherwise,
B. if the input buffer begins with a prefix of "/./" or "/.", where "." is a complete path segment, then replace that prefix with "/" in the input buffer; otherwise,
C. if the input buffer begins with a prefix of "/../" or "/..", where ".." is a complete path segment, then replace that prefix with "/" in the input buffer and remove the last segment and its preceding "/" (if any) from the output buffer; otherwise,
D. if the input buffer consists only of "." or "..", then remove that from the input buffer; otherwise,
E. move the first path segment in the input buffer to the end of the output buffer, including the initial "/" character (if any) and any subsequent characters up to, but not including, the next "/" character or the end of the input buffer.
It works by remembering the position of the last valid item and then if you get dots then removing the item. The full description is here
HERE IS MY VERSION OF IT IN C++...
This is a more simple problem then you are thinking about it. All you need to do is
explode()
on the/
character, and parse out all of the individual segments using a stack. As you traverse the array from left to right, if you see.
, do nothing. If you see..
, pop an element from the stack. Otherwise, push an element onto the stack.This will properly resolve the URLs in all of your test cases.
Note that error checking is left as an exercise to the user (i.e. when the
$parents
stack is empty and you try to pop something off of it).