Created
March 29, 2012 21:41
-
-
Save chdemko/2244092 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function testParse_Url() | |
{ | |
$url = 'http://localhost/joomla_development/j16_trunk/administrator/index.php?option=com_contact&view=contact&layout=edit&id=5'; | |
$expected = parse_url($url); | |
$actual = JString::parse_url($url); | |
$this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); | |
$url = 'http://joomla.org/mytestpath/È'; | |
$expected = parse_url($url); | |
// Fix up path for UTF-8 characters | |
$expected['path'] = '/mytestpath/È'; | |
$actual = JString::parse_url($url); | |
$this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); | |
// Test special characters in URL | |
$url = 'http://mydomain.com/!*\'();:@&=+$,/?%#[]'; | |
$expected = parse_url($url); | |
$actual = JString::parse_url($url); | |
$this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); | |
// Test rawurlencode in query | |
$url = 'http://a.com/b.php?retUrl='.rawurlencode('http://a.com/b.php?a=123&b=456'); | |
$expected = parse_url($url); | |
$actual = JString::parse_url($url); | |
$this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); | |
// Test fragment | |
$url = 'http://example.com/test# _22'; | |
$expected = parse_url($url); | |
$actual = JString::parse_url($url); | |
$this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); | |
// Test password | |
$url = 'ftp://user:abcé@example.com/'; | |
$expected = parse_url($url); | |
$actual = JString::parse_url($url); | |
$this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); | |
// Test utf-8 in vars | |
$url = 'http://a.com/?a=é'; | |
$expected = parse_url($url); | |
$actual = JString::parse_url($url); | |
$this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); | |
// Test chinese | |
$url = 'http://mydomain.com/path/道'; | |
$expected = parse_url($url); | |
$actual = JString::parse_url($url); | |
$this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); | |
// Test wrong url | |
$url = 'http://example.com:###'; | |
$expected = parse_url($url); | |
$actual = JString::parse_url($url); | |
$this->assertEquals($expected, $actual, 'Line: ' . __LINE__ . ' Results should be equal'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static function parse_url($url) | |
{ | |
$result = array(); | |
// Build arrays of values we need to decode before parsing | |
$entities = array('%3A', '%40', '%26', '%3D', '%2F', '%3F', '%23', '%5B', '%5D'); | |
$replacements = array(":", "@", "&", "=", "/", "?", "#", "[", "]"); | |
// Create encoded URL with special URL characters decoded so it can be parsed | |
// All other characters will be encoded | |
$encodedURL = str_replace($entities, $replacements, urlencode($url)); | |
// Parse the encoded URL | |
$encodedParts = parse_url($encodedURL); | |
// Now, decode each value of the resulting array | |
foreach ($encodedParts as $key => $value) | |
{ | |
$result[$key] = urldecode($value); | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment