wp_parse_url

A wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7.

Description

(bool|array) wp_parse_url( (string) $url ); 

PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including schemeless and relative url's with :// in the path, this works around those limitations providing a standard output on PHP 5.2~5.4+.

Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated when URL parsing failed.

Returns (bool|array)

False on failure; Array of URL components on success; See parse_url()'s return values.

Parameters (1)

0. $url (string)
The URL to parse.

Usage

  1. if ( !function_exists( 'wp_parse_url' ) ) { 
  2. require_once ABSPATH . WPINC . '/http.php'; 
  3.  
  4. // The URL to parse. 
  5. $url = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_parse_url($url); 
  9.  

Defined (1)

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

/wp-includes/http.php  
  1. function wp_parse_url( $url ) { 
  2. $parts = @parse_url( $url ); 
  3. if ( ! $parts ) { 
  4. // < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path 
  5. if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) { 
  6. // Since we know it's a relative path, prefix with a scheme/host placeholder and try again 
  7. if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) { 
  8. return $parts; 
  9. // Remove the placeholder values 
  10. unset( $parts['scheme'], $parts['host'] ); 
  11. } else { 
  12. return $parts; 
  13.  
  14. // < PHP 5.4.7 compat, doesn't detect schemeless URL's host field 
  15. if ( '//' == substr( $url, 0, 2 ) && ! isset( $parts['host'] ) ) { 
  16. $path_parts = explode( '/', substr( $parts['path'], 2 ), 2 ); 
  17. $parts['host'] = $path_parts[0]; 
  18. if ( isset( $path_parts[1] ) ) { 
  19. $parts['path'] = '/' . $path_parts[1]; 
  20. } else { 
  21. unset( $parts['path'] ); 
  22.  
  23. return $parts;