You are here

public static function UrlHelper::isValid in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Component/Utility/UrlHelper.php \Drupal\Component\Utility\UrlHelper::isValid()
  2. 9 core/lib/Drupal/Component/Utility/UrlHelper.php \Drupal\Component\Utility\UrlHelper::isValid()

Verifies the syntax of the given URL.

This function should only be used on actual URLs. It should not be used for Drupal menu paths, which can contain arbitrary characters. Valid values per RFC 3986.

Parameters

string $url: The URL to verify.

bool $absolute: Whether the URL is absolute (beginning with a scheme such as "http:").

Return value

bool TRUE if the URL is in a valid format, FALSE otherwise.

10 calls to UrlHelper::isValid()
DbLogController::createLink in core/modules/dblog/src/Controller/DbLogController.php
Creates a Link object if the provided URI is valid.
Endpoint::__construct in core/modules/media/src/OEmbed/Endpoint.php
Endpoint constructor.
PathValidator::getUrl in core/lib/Drupal/Core/Path/PathValidator.php
Helper for getUrlIfValid() and getUrlIfValidWithoutAccessCheck().
Provider::__construct in core/modules/media/src/OEmbed/Provider.php
Provider constructor.
Url::validateUrl in core/lib/Drupal/Core/Render/Element/Url.php
Form element validation handler for #type 'url'.

... See full list

File

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

Class

UrlHelper
Helper class URL based methods.

Namespace

Drupal\Component\Utility

Code

public static function isValid($url, $absolute = FALSE) {
  if ($absolute) {
    return (bool) preg_match("\n        /^                                                      # Start at the beginning of the text\n        (?:ftp|https?|feed):\\/\\/                                # Look for ftp, http, https or feed schemes\n        (?:                                                     # Userinfo (optional) which is typically\n          (?:(?:[\\w\\.\\-\\+!\$&'\\(\\)*\\+,;=]|%[0-9a-f]{2})+:)*      # a username or a username and password\n          (?:[\\w\\.\\-\\+%!\$&'\\(\\)*\\+,;=]|%[0-9a-f]{2})+@          # combination\n        )?\n        (?:\n          (?:[a-z0-9\\-\\.]|%[0-9a-f]{2})+                        # A domain name or a IPv4 address\n          |(?:\\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\\])         # or a well formed IPv6 address\n        )\n        (?::[0-9]+)?                                            # Server port number (optional)\n        (?:[\\/|\\?]\n          (?:[\\w#!:\\.\\?\\+=&@\$'~*,;\\/\\(\\)\\[\\]\\-]|%[0-9a-f]{2})   # The path and query (optional)\n        *)?\n      \$/xi", $url);
  }
  else {
    return (bool) preg_match("/^(?:[\\w#!:\\.\\?\\+=&@\$'~*,;\\/\\(\\)\\[\\]\\-]|%[0-9a-f]{2})+\$/i", $url);
  }
}