1 link.module link_cleanup_url($url, $protocol = 'http')

Forms a valid URL if possible from an entered address.

Trims whitespace and automatically adds an http:// to addresses without a protocol specified

Parameters

string $url: The URL entered by the user.

string $protocol: The protocol to be prepended to the URL if one is not specified.

Return value

string: The URL with the protocol prepended if needed.

File

core/modules/link/link.module, line 1025
Defines simple link field types.

Code

function link_cleanup_url($url, $protocol = 'http') {
  $url = trim($url);
  $type = link_validate_url($url);

  if ($type === LINK_EXTERNAL) {
    // Check if there is no protocol specified.
    $protocol_match = preg_match('/^([a-z0-9][a-z0-9\.\-_]*:\/\/)/i', $url);
    if (empty($protocol_match)) {
      // But should there be? Add an automatic http:// if it starts with a domain name.
      $domain_match = preg_match('/^(([a-z0-9]([a-z0-9\-_]*\.){1,63})([a-z]{2,63}))/i', $url);
      if (!empty($domain_match)) {
        $url = $protocol . "://" . $url;
      }
    }
  }

  return $url;
}