1 redirect.module redirect_parse_url($url)

Parse URLs into component parts: fragment, query, protocol, and URL.

This function extends the PHP parse_url() function. In addition to the return array normally provided by that function, this does additional parsing, including:

  • Removing the hash symbol from the "fragment" value.
  • Converting the "query" value into an array instead of a string.
  • Adds an "https" value indicating an https scheme.
  • Adds a "url" value that is suitable for passing into the url() function. If given a full URL of the current site, the hostname, scheme, and port will be stripped from this value. External URLs will be unaffected.

Parameters

string $url.:

Return value

array $parsed.: An array with the following keys:

  • scheme: Usually "http" or "https".
  • host: The hostname, e.g. "example.com".
  • port: The port number.
  • user: Any HTTP user in a URL such as user:pass@example.com.
  • pass: Any HTTP password that is used with the "user".
  • path: The path portion of the URL.
  • query: An array of query string parameters.
  • fragment: Any string after the hash (#) symbol.
  • url: A local internal Backdrop path or a full external URL.
  • https: Whether the URL starts with "https".

File

core/modules/redirect/redirect.module, line 1189

Code

function redirect_parse_url($url) {
  $original_url = $url;
  $url = trim($url, " \t\n\r\0\x0B\/");
  $parsed = parse_url($url);

  if (isset($parsed['fragment'])) {
    $url = substr($url, 0, -strlen($parsed['fragment']));
    $url = trim($url, '#');
  }
  if (isset($parsed['query'])) {
    $url = substr($url, 0, -strlen($parsed['query']));
    $url = trim($url, '?&');
    $parsed['query'] = backdrop_get_query_array($parsed['query']);
  }

  // Convert absolute to relative.
  if (isset($parsed['scheme']) && isset($parsed['host'])) {
    $base_secure_url = rtrim($GLOBALS['base_secure_url'] . base_path(), '/');
    $base_insecure_url = rtrim($GLOBALS['base_insecure_url'] . base_path(), '/');
    if (strpos($url, $base_secure_url) === 0) {
      $url = str_replace($base_secure_url, '', $url);
      $parsed['https'] = TRUE;
    }
    elseif (strpos($url, $base_insecure_url) === 0) {
      $url = str_replace($base_insecure_url, '', $url);
    }
  }

  $url = trim($url, '/');

  // Convert to frontpage paths.
  if ($url == '<front>') {
    $url = '';
  }

  $parsed['url'] = $url;

  // Allow modules to alter the parsed URL.
  backdrop_alter('redirect_parse_url', $parsed, $original_url);

  return $parsed;
}