youtube_sanitize_url

Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands.

Description

(string) youtube_sanitize_url( (string) $url ); 

Returns (string)

The normalized URL

Parameters (1)

0. $url (string)
The url.

Usage

  1. if ( !function_exists( 'youtube_sanitize_url' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/functions.compat.php'; 
  3.  
  4. // The url. 
  5. $url = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = youtube_sanitize_url($url); 
  9.  

Defined (2)

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

/functions.compat.php  
  1. function youtube_sanitize_url( $url ) { 
  2. $url = trim( $url, ' "' ); 
  3. $url = trim( $url ); 
  4. $url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&', '&', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url ); 
  5.  
  6. // Replace any extra question marks with ampersands - the result of a URL like "http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US" being passed in. 
  7. $query_string_start = strpos( $url, "?" ); 
  8.  
  9. if ( false !== $query_string_start ) { 
  10. $url = substr( $url, 0, $query_string_start + 1 ) . str_replace( "?", "&", substr( $url, $query_string_start + 1 ) ); 
  11.  
  12. return $url; 
/modules/shortcodes/youtube.php  
  1. function youtube_sanitize_url( $url ) { 
  2. $url = trim( $url, ' "' ); 
  3. $url = trim( $url ); 
  4. $url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&', '&', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url ); 
  5.  
  6. // Replace any extra question marks with ampersands - the result of a URL like "http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US" being passed in. 
  7. $query_string_start = strpos( $url, "?" ); 
  8.  
  9. if ( false !== $query_string_start ) { 
  10. $url = substr( $url, 0, $query_string_start + 1 ) . str_replace( "?", "&", substr( $url, $query_string_start + 1 ) ); 
  11.  
  12. return $url;