valid_url

Versions
4.6 – 7
valid_url($url, $absolute = FALSE)

Verify 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 functions call valid_url()

aggregator_form_feed_validate in modules/aggregator/aggregator.admin.inc
Validate aggregator_form_feed() form submissions.
aggregator_form_opml_submit in modules/aggregator/aggregator.admin.inc
Process aggregator_form_opml form submissions.
aggregator_form_opml_validate in modules/aggregator/aggregator.admin.inc
Validate aggregator_form_opml form submissions.
comment_form_validate in modules/comment/comment.module
Validate comment form submissions.
profile_user_form_validate in modules/profile/profile.module
Form validation handler for the user register/profile form.
update_manager_install_form_validate in modules/update/update.manager.inc
Validate the form for installing a new project via the update manager.

Code

includes/common.inc, line 1587

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

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.