function UrlHelper::parse

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Utility/UrlHelper.php \Drupal\Component\Utility\UrlHelper::parse()
  2. 8.9.x core/lib/Drupal/Component/Utility/UrlHelper.php \Drupal\Component\Utility\UrlHelper::parse()
  3. 10 core/lib/Drupal/Component/Utility/UrlHelper.php \Drupal\Component\Utility\UrlHelper::parse()

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

This function splits both internal paths like

 node?b=c#d 

and external URLs like

https:

//example.com/a?b=c#d

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.

Parameters

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

\Drupal\Core\Utility\LinkGenerator

http://tools.ietf.org/html/rfc3986

Related topics

31 calls to UrlHelper::parse()
AssetOptimizationTest::invalidExclude in core/tests/Drupal/FunctionalTests/Asset/AssetOptimizationTest.php
Adds an invalid 'exclude' query parameter with an invalid value.
AssetOptimizationTest::invalidInclude in core/tests/Drupal/FunctionalTests/Asset/AssetOptimizationTest.php
Replaces the 'include' query parameter with an invalid value.
AssetOptimizationTest::omitInclude in core/tests/Drupal/FunctionalTests/Asset/AssetOptimizationTest.php
Removes the 'include' query parameter from the given URL.
AssetOptimizationTest::omitTheme in core/tests/Drupal/FunctionalTests/Asset/AssetOptimizationTest.php
Removes the 'theme' query parameter from the given URL.
AssetOptimizationTest::replaceGroupDelta in core/tests/Drupal/FunctionalTests/Asset/AssetOptimizationTest.php
Replaces the delta in the given URL.

... See full list

File

core/lib/Drupal/Component/Utility/UrlHelper.php, line 183

Class

UrlHelper
Helper class URL based methods.

Namespace

Drupal\Component\Utility

Code

public static function parse($url) {
    $options = [
        'path' => NULL,
        'query' => [],
        '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.
    // The URL is considered external if it contains the '://' delimiter. Since
    // a URL can also be passed as a query argument, we check if this delimiter
    // appears in front of the '?' query argument delimiter.
    $scheme_delimiter_position = strpos($url, '://');
    $query_delimiter_position = strpos($url, '?');
    if ($scheme_delimiter_position !== FALSE && ($query_delimiter_position === FALSE || $scheme_delimiter_position < $query_delimiter_position)) {
        // Split off the fragment, if any.
        if (str_contains($url, '#')) {
            [
                $url,
                $options['fragment'],
            ] = explode('#', $url, 2);
        }
        // Split off everything before the query string into 'path'.
        $parts = explode('?', $url, 2);
        // Don't support URLs without a path, like 'http://'.
        [
            ,
            $path,
        ] = explode('://', $parts[0], 2);
        if ($path != '') {
            $options['path'] = $parts[0];
        }
        // If there is a query string, transform it into keyed query parameters.
        if (isset($parts[1])) {
            parse_str($parts[1], $options['query']);
        }
    }
    else {
        // parse_url() does not support relative URLs, so make it absolute. For
        // instance, 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'];
        }
    }
    return $options;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.