Same name and namespace in other branches
- 4.6.x includes/common.inc \valid_url()
- 4.7.x includes/common.inc \valid_url()
- 5.x includes/common.inc \valid_url()
- 6.x includes/common.inc \valid_url()
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
$url: The URL to verify.
$absolute: Whether the URL is absolute (beginning with a scheme such as "http:").
Return value
TRUE if the URL is in a valid format.
Related topics
6 calls to valid_url()
- comment_form_validate in modules/
comment/ comment.module - Validate comment form submissions.
- CommonURLUnitTest::testDrupalParseUrl in modules/
simpletest/ tests/ common.test - Test drupal_parse_url().
- ValidUrlTestCase::testInvalidAbsolute in modules/
simpletest/ tests/ common.test - Test invalid absolute URLs.
- ValidUrlTestCase::testInvalidRelative in modules/
simpletest/ tests/ common.test - Test invalid relative URLs.
- ValidUrlTestCase::testValidAbsolute in modules/
simpletest/ tests/ common.test - Test valid absolute URLs.
File
- includes/
common.inc, line 1312 - Common functions that many Drupal modules will need to reference.
Code
function valid_url($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);
}
}