1 filter.pages.inc _filter_create_valid_external_url($url)

Forms a valid external URL if possible.

Automatically adds an http:// to addresses that appear to be external URLs but have no specified protocol.


string $url: The URL entered by the user.

Return value

string: The URL with the protocol prepended if needed.

See also



core/modules/filter/filter.pages.inc, line 619
User page callbacks for the Filter module.


function _filter_create_valid_external_url($url) {
  // Skip if it's already a valid external URL with a protocol.
  if (_filter_url_has_protocol($url)) {
    return $url;

  // If it looks like a domain name but it's missing a protocol, add a protocol.
  // The will catch domains like archive.org but will skip a valid external
  // domains with a relative protocol like //archive.org.
  $domain_match = preg_match('/^(([a-z0-9]([a-z0-9\-_]*\.){1,63})([a-z]{2,63}))/i', $url);
  if (!empty($domain_match)) {
    // We add a relative protocol since we don't know if the address requires
    // http or https.
    $url = '//' . $url;

  return $url;