clean_url

Checks and cleans a URL.

Description

(string) clean_url( (string) $url, (constant) $protocols = null, (string) $context = 'display' ); 

A number of characters are removed from the URL. If the URL is for displaying (the default behaviour) amperstands are also replaced. The esc_url filter is applied to the returned cleaned URL.

Returns (string)

The cleaned $url after the 'cleaned_url' filter is applied.

Parameters (3)

0. $url (string)
The URL to be cleaned.
1. $protocols — Optional. (constant) => null
An array of acceptable protocols. Defaults to http,, https , ftp, ftps, mailto, news, irc, gopher, nntp, feed, telnet, mms, rtsp, svn if not set.
2. $context — Optional. (string) => 'display'
How the URL will be used. Default is display..

Usage

  1. if ( !function_exists( 'clean_url' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/backpress/functions.formatting.php'; 
  3.  
  4. // The URL to be cleaned. 
  5. $url = ''; 
  6.  
  7. // Optional. An array of acceptable protocols. 
  8. // Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn' if not set. 
  9. $protocols = null; 
  10.  
  11. // Optional. How the URL will be used. Default is 'display'. 
  12. $context = 'display'; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = clean_url($url, $protocols, $context); 
  16.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bbpress/bb-includes/backpress/functions.formatting.php  
  1. function clean_url( $url, $protocols = null, $context = 'display' ) { 
  2. $original_url = $url; 
  3.  
  4. if ( '' == $url ) 
  5. return $url; 
  6. $url = preg_replace('|[^a-z0-9-~+_.?#=!&;, /:%@$\|*\'()\\x80-\\xff]|i', '', $url); 
  7. $strip = array('%0d', '%0a', '%0D', '%0A'); 
  8. $url = _deep_replace($strip, $url); 
  9. $url = str_replace(';//', '://', $url); 
  10. /** If the URL doesn't appear to contain a scheme, we 
  11. * presume it needs http:// appended (unless a relative 
  12. * link starting with / or a php file). 
  13. */ 
  14. if ( strpos($url, ':') === false && 
  15. substr( $url, 0, 1 ) != '/' && substr( $url, 0, 1 ) != '#' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) ) 
  16. $url = 'http://' . $url; 
  17.  
  18. // Replace ampersands and single quotes only when displaying. 
  19. if ( 'display' == $context ) { 
  20. $url = wp_kses_normalize_entities( $url ); 
  21. $url = str_replace( '&', '&', $url ); 
  22. $url = str_replace( "'", ''', $url ); 
  23.  
  24. if ( !is_array($protocols) ) 
  25. $protocols = array ('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn'); 
  26. if ( wp_kses_bad_protocol( $url, $protocols ) != $url ) 
  27. return ''; 
  28.  
  29. return apply_filters('clean_url', $url, $original_url, $context);