You are here

function url_remove_dot_segments in TMGMT Translator Smartling 8

Filter out "." and ".." segments from a URL's path and return the result.

This function implements the "remove_dot_segments" algorithm from the RFC3986 specification for URLs.

This function supports multi-byte characters with the UTF-8 encoding, per the URL specification.

Parameters: path the path to filter

Return values: The filtered path with "." and ".." removed.

1 call to url_remove_dot_segments()
url_to_absolute in includes/url_to_absolute.inc
Combine a base URL and a relative URL to produce a new absolute URL. The base URL is often the URL of a page, and the relative URL is a URL embedded on that page.

File

includes/url_to_absolute.inc, line 156
Edited by Nitin Kr. Gupta, publicmind.in

Code

function url_remove_dot_segments($path) {

  // multi-byte character explode
  $in_segs = preg_split('!/!u', $path);
  $out_segs = array();
  foreach ($in_segs as $seg) {
    if ($seg == '' || $seg == '.') {
      continue;
    }
    if ($seg == '..') {
      array_pop($out_segs);
    }
    else {
      array_push($out_segs, $seg);
    }
  }
  $out_path = implode('/', $out_segs);
  if ($path[0] == '/') {
    $out_path = '/' . $out_path;
  }

  // compare last multi-byte character against '/'
  if ($out_path != '/' && mb_strlen($path) - 1 == mb_strrpos($path, '/', 'UTF-8')) {
    $out_path .= '/';
  }
  return $out_path;
}