1 <?php
2 /**
3 * Part of the Joomla Framework Uri Package
4 *
5 * @copyright Copyright (C) 2005 - 2016 Open Source Matters. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE
7 */
8
9 namespace Joomla\Uri;
10
11 /**
12 * Uri Helper
13 *
14 * This class provides a UTF-8 safe version of parse_url().
15 *
16 * @since 1.0
17 */
18 class UriHelper
19 {
20 /**
21 * Does a UTF-8 safe version of PHP parse_url function
22 *
23 * @param string $url URL to parse
24 *
25 * @return mixed Associative array or false if badly formed URL.
26 *
27 * @see http://us3.php.net/manual/en/function.parse-url.php
28 * @since 1.0
29 */
30 public static function parse_url($url)
31 {
32 $result = false;
33
34 // Build arrays of values we need to decode before parsing
35 $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%24', '%2C', '%2F', '%3F', '%23', '%5B', '%5D');
36 $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "$", ",", "/", "?", "#", "[", "]");
37
38 // Create encoded URL with special URL characters decoded so it can be parsed
39 // All other characters will be encoded
40 $encodedURL = str_replace($entities, $replacements, urlencode($url));
41
42 // Parse the encoded URL
43 $encodedParts = parse_url($encodedURL);
44
45 // Now, decode each value of the resulting array
46 if ($encodedParts)
47 {
48 foreach ($encodedParts as $key => $value)
49 {
50 $result[$key] = urldecode(str_replace($replacements, $entities, $value));
51 }
52 }
53
54 return $result;
55 }
56 }
57