Created
January 19, 2016 15:28
-
-
Save JanPetterMG/3f204da4085e626bbb91 to your computer and use it in GitHub Desktop.
URL encoder according to RFC 3986
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
/** | |
* URL encoder according to RFC 3986 | |
* Returns a string containing the encoded URL with disallowed characters converted to their percentage encodings. | |
* @link http://publicmind.in/blog/url-encoding/ | |
* | |
* @param $url | |
* @return string | |
*/ | |
function encode_url($url) | |
{ | |
$reserved = array( | |
":" => '!%3A!ui', | |
"/" => '!%2F!ui', | |
"?" => '!%3F!ui', | |
"#" => '!%23!ui', | |
"[" => '!%5B!ui', | |
"]" => '!%5D!ui', | |
"@" => '!%40!ui', | |
"!" => '!%21!ui', | |
"$" => '!%24!ui', | |
"&" => '!%26!ui', | |
"'" => '!%27!ui', | |
"(" => '!%28!ui', | |
")" => '!%29!ui', | |
"*" => '!%2A!ui', | |
"+" => '!%2B!ui', | |
"," => '!%2C!ui', | |
";" => '!%3B!ui', | |
"=" => '!%3D!ui', | |
"%" => '!%25!ui', | |
); | |
$url = rawurlencode($url); | |
$url = preg_replace(array_values($reserved), array_keys($reserved), $url); | |
return $url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment