Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Удаляет сегменты с точками из пути.
- *
- * Алгоритм описан по ссылке:
- * https://tools.ietf.org/html/rfc3986#section-5.2.4
- *
- */
- function removeDotSegments($path) {
- $input = $path;
- $out = array();
- while (strlen($input)) {
- // A. If the input buffer begins with a prefix of "../" or "./",
- // then remove that prefix from the input buffer; otherwise,
- $input = preg_replace('#^[.][.]?/#', '/', $input);
- // 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,
- if (substr($input, 0, 3) === '/./') {
- $out[] = '/';
- $input = substr($input, 2);
- continue;
- }
- if ($input == '/.') {
- $out[] = '/';
- break;
- }
- // 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,
- if (substr($input, 0, 4) === '/../') {
- array_pop($out);
- $input = substr($input, 3);
- continue;
- }
- if ($input === '/..') {
- $out[] = '/';
- break;
- }
- // D. if the input buffer consists only of "." or "..", then remove
- // that from the input buffer; otherwise,
- if ($input === '.' || $input === '..') {
- break;
- }
- // 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.
- $pos = strpos($input, '/', 1);
- if ($pos === false) {
- $out[] = $input;
- break;
- }
- $out[] = substr($input, 0, $pos);
- $input = substr($input, $pos);
- }
- return implode($out);
- }
- // STEP OUTPUT BUFFER INPUT BUFFER
- //
- // 1 : /a/b/c/./../../g
- // 2E: /a /b/c/./../../g
- // 2E: /a/b /c/./../../g
- // 2E: /a/b/c /./../../g
- // 2B: /a/b/c /../../g
- // 2C: /a/b /../g
- // 2C: /a /g
- // 2E: /a/g
- var_dump(removeDotSegments('/a/b/c/./../../g'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement