7.x common.inc drupal_parse_url($url)

Parses a URL string into its path, query, and fragment components.

This function splits both internal paths like


and external URLs like



into their component parts. See RFC 3986 for an explanation of what the component parts are.

Note that, unlike the RFC, when passed an external URL, this function groups the scheme, authority, and path together into the path component.


string $url: The internal path or external URL string to parse.

Return value

array An associative array containing:

  • path: The path component of $url. If $url is an external URL, this includes the scheme, authority, and path.
  • query: An array of query parameters from $url, if they exist.
  • fragment: The fragment component from $url, if it exists.

See also





Related topics

6 calls to drupal_parse_url()
CommonURLUnitTest::testDrupalParseUrl in modules/simpletest/tests/common.test
Test drupal_parse_url().
confirm_form in modules/system/system.module
Generates a form array for a confirmation form.
DrupalWebTestCase::curlExec in modules/simpletest/drupal_web_test_case.php
Initializes and executes a cURL request.
drupal_goto in includes/common.inc
Sends the user to a different page.
field_ui_get_destinations in modules/field_ui/field_ui.admin.inc
Extracts next redirect path from an array of multiple destinations.

... See full list


includes/common.inc, line 575
Common functions that many Drupal modules will need to reference.


function drupal_parse_url($url) {
  $options = array(
    'path' => NULL,
    'query' => array(),
    'fragment' => '',

  // External URLs: not using parse_url() here, so we do not have to rebuild
  // the scheme, host, and path without having any use for it.
  if (strpos($url, '://') !== FALSE) {

    // Split off everything before the query string into 'path'.
    $parts = explode('?', $url);
    $options['path'] = $parts[0];

    // If there is a query string, transform it into keyed query parameters.
    if (isset($parts[1])) {
      $query_parts = explode('#', $parts[1]);
      parse_str($query_parts[0], $options['query']);

      // Take over the fragment, if there is any.
      if (isset($query_parts[1])) {
        $options['fragment'] = $query_parts[1];
  else {

    // parse_url() does not support relative URLs, so make it absolute. E.g. the
    // relative URL "foo/bar:1" isn't properly parsed.
    $parts = parse_url('http://example.com/' . $url);

    // Strip the leading slash that was just added.
    $options['path'] = substr($parts['path'], 1);
    if (isset($parts['query'])) {
      parse_str($parts['query'], $options['query']);
    if (isset($parts['fragment'])) {
      $options['fragment'] = $parts['fragment'];

  // The 'q' parameter contains the path of the current page if clean URLs are
  // disabled. It overrides the 'path' of the URL when present, even if clean
  // URLs are enabled, due to how Apache rewriting rules work.
  if (isset($options['query']['q'])) {
    $options['path'] = $options['query']['q'];
  return $options;


exlin’s picture

If there are issues with language prefixes, check out http://api.drupal.org/api/drupal/includes!path.inc/function/current_path/7 if that works for you

jay.dansand’s picture

If using drupal_parse_url() for external URLs, the query string is run through PHP's parse_str() function. parse_str() changes certain invalid variable name characters to underscores "_". See PHP.net: Variables From External Sources for more information on why this alteration happens.


$url = 'http://example.com?foo.bar=baz';
$parsed = drupal_parse_url($url);
/* Output (note "foo.bar" became "foo_bar"):
array(1) {
  string(3) "baz"

See the question and answer StackOverflow: Get PHP to stop replacing '.' characters in $_GET or $_POST arrays? for a workaround.

nevergone’s picture

Drupal 8: public static function UrlHelper::parse()